0

I have written so code to query lists of users who go jogging at a certain time and in certain locations, but I am getting the following error: ActiveRecord::StatementInvalid: SQLite3::SQLException: near "'jogging'"

Does this mean I cannot write a string into that variable? Are there any solutions to this?

users = User.where('ids in (:user_ids)', user_ids: 
             Activity.where('title :title AND created_at >= :created_at AND location_id: location_id', 
             {title: 'jogging', created_at: Time.zone.now.beginning_of_day, location_id: 
             Location.where('id in id', id: user.activities.where.not(location_id: nil)
            .order('created_at DESC').first)}))
3
  • 2
    Tip: don't write long queries in one huge line. It's very easy to become lost and stuck at errors like that. Split this huge query into smaller pieces using intermediate variables, and you will be able to see the syntax error by your own. Commented Feb 1, 2018 at 8:52
  • title = :title instead of title :title i guess Commented Feb 1, 2018 at 9:36
  • @nattfodd could you give me some examples, I'm new to this. Thank you. Commented Feb 1, 2018 at 10:15

2 Answers 2

2

You can simplified your query this way

location_id = Location.where(
                             id: user.activities
                                     .where.not(location_id: nil)
                                     .order('created_at DESC').first
                            )

user_ids    = Activity.where("title = ? AND created_at > ? AND location_id = ?", 
                             "jogging", Time.zone.now.beginning_of_day, location_id)

users       = User.where(id: user_ids)

But If you want to keep query one liner. You can use this

User.where('id IN(:user_ids)', 
            user_ids:  Activity.where('title = :title AND created_at >= :created_at AND location_id = :location_id', title: 'jogging', created_at: Time.zone.now.beginning_of_day, 
            location_id: Location.where('id IN(:id)', id: user.activities.where.not(location_id: nil)
            .order('created_at DESC').first).ids)
          )

Note: I am assuming that you have user object to use above one liner query

Hope this will help you.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you forgot to add an = in the query:

Your query:

Activity.where('title :title ...

What you want:

Activity.where('title = :title ...

And if you don't need an operator like > or <, you can also use:

Activity.where(title: title)

If you then need to chain it, it's pretty simple:

Activity.where(title: title).where('foo < ?', 100)

2 Comments

should I add = to everyquery? because I have location_id also
@chronycles yes, I think you should lookup SQL query syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.