I am trying to do a simple or statement in a controller
This generates one set of trips that I am interested in displaying and is working fine.
#trips = Trip.where(:userid => #friends)
However, i would like to add another set of trips; trips whose userid == current_user.id
#trips = Trip.where(:userid => current_user.id)
Trying to combine these two i tried...
#trips = Trip.where(:conditions => ['userid= ? OR userid=?', #friends, current_user.id])
Any idea where the bust is? Thanks in advance.
Simply pass an array to get the equivalent of the SQL WHERE ... IN clause.
Trip.where(userid: [#friends, current_user.id])
See the ActiveRecord Rails Guide, 2.3.3 Subset Conditions
Is #friends an array? ActiveRecord will convert your where statement into a SQL IN clause when you generate it with the hash syntax. When you build your OR statement in raw SQL, you need to do the IN manually.
#trips = Trip.where('userid IN ? OR userid = ?', #friends, current_user.id)
Also, your column is called userid ? Typicaly it would be called user_id - is this a typo or do you just have an abnormal database?
Related
I'm building a Sinatra application using three database's tables: user, post and like.
I'd want to run a query that will find an entry in the like table like so:
FIND in like WHERE user_id == params[:user_id] AND post_id == params[:post_id]
(for one condition I'll be using: Like.find_by_user_id(params[:user_id]))
My question is:
How to run a find query with multiple conditions using the ActiveRecord Gem?
Use where:
Like.where('user_id = ? AND post_id = ?', params[:user_id], params[:post_id])
or
Like.where('user_id = :user_id AND post_id = :post_id', params)
Is important to keep in mind that the paremeters of the where need to be converted to the expected type for example params[:post_id].to_i
Similar to find_by_user_id for user_id column you can combine multiple column names and get a dynamic finder find_by_user_id_and_post_id:
Like.find_by_user_id_and_post_id(params[:user_id], params[:post_id])
When there are more than "bearable" columns in the find_by_ finder, you could use where and supply the condition as follows:
Like.where(user_id: params[:user_id], post_id: params[:post_id])
Like.find_by_user_id(params[:user_id]) - this syntax is deprecated in ActiveRecord 4.
Instead try using where method of ActiveRecord query interface, to pass array conditions. Example:
Like.where("user_id = ? AND post_id = ?", params[:user_id], params[:post_id])
If you are expecting one record to be the result:
To replace find_by_whatever you can use find_by(whatever) for example User.find_by(username:"UsernameIsMyUsername",password:"Mypassword"). You should use find_by if there is only one record that you expect to match your search.
If you are expecting more than one record to be the result:
If you expect more than one you should use where with where(username:"MyUsername",password:"Password"). This will return all the resulting records in an array.
Is it possible to use the '.where()' controller method to select all db-elements that are not equal to an array consisting of ID numbers?
I can easily use the '.where()' controller method to select all db elements that are equal to a list of ID numbers stored in an array:
user_event_ids = current_user.event_items.pluck(:event_id)
#user_events = Event.where(id: user_match_ids)
But I can't seem to find a way to select all db-elements that have ID numbers which ar enot in the 'user_event_ids' array.
I've tried (with no luck):
#non_user_events = Event.where('user_id not in :arr', {arr: user_event_ids})
#non_user_events = Event.where(''user_id != ?', user_event_ids)
Both do not work and cancel with errors. Any help is appreciated!
This should be the correct syntax for what you are after:
Event.where('user_id NOT IN (?)', user_event_ids)
Given excluded_ids is an Array containing the IDs you want to exclude
If you are using Rails 4:
Event.where.not(user_id: excluded_ids)
If you are using Rails 3:
Event.where('user_id NOT IN (?)', user_event_ids)
If you are using Squeel
Event.where{user_id.not_in excluded_ids}
Here is another syntax:
Event.find(:all, :conditions => ['user_id not in (?)', user_event_ids])
I have a hash with members of an appointment and try to create a MySQL query in my rails controller.
The users with the ids from the map command e.g. {1,3} should be excluded in the query.
So for example #appointment.members.map{|m| m.user_id} returns {1,3,5} and I'd like to find all other users but these.
#users = User.where( "id NOT IN (?)", #appointment.members.map{|m| m.user_id} )
I'm using Rails 3.2.9 so maybe my exclude statement is wrong cause I don't get any result out of this query. The problem can't be the map array - I already tested that.
Thanks in advance.
I think you just have a problem with the map method, try this:
#users = User.where( "id NOT IN (?)", #appointment.members.map(&:user_id) )
EDIT: removed the wrong syntax for future readers if any
I solved the problem with a new instance variable:
#members = #appointment.members.all
and then
#add_users = User.where( "id NOT IN (?)", #members.map(&:user_id) )
thanks for the support
I'll explain this as best as possible. I have a query on user posts:
#selected_posts = Posts.where(:category => "Baseball")
I would like to write the following statement. Here it is in pseudo terms:
User.where(user has a post in #selected_posts)
Keep in mind that I have a many to many relationship setup so post.user is usable.
Any ideas?
/EDIT
#posts_matches = User.includes(#selected_posts).map{ |user|
[user.company_name, user.posts.count, user.username]
}.sort
Basically, I need the above to work so that it uses the users that HAVE posts in selected_posts and not EVERY user we have in our database.
Try this:
user.posts.where("posts.category = ?", "Baseball")
Edit 1:
user.posts.where("posts.id IN (?)", #selected_posts)
Edit 2:
User.select("users.company_name, count(posts.id) userpost_count, user.username").
joins(:posts).
where("posts.id IN (?)", #selected_posts).
order("users.company_name, userpost_count, user.username")
Just use the following:
User.find(#selected_posts.map(&:user_id).uniq)
This takes the user ids from all the selected posts, turns them into an array, and removes any duplicates. Passing an array to user will just find all the users with matching ids. Problem solved.
To combine this with what you showed in your question, you could write:
#posts_matches = User.find(#selected_posts.map(&:user_id).uniq).map{ |user|
[user.company_name, user.posts.size, user.username]
}
Use size to count a relation instead of count because Rails caches the size method and automatically won't look it up more than once. This is better for performance.
Not sure what you were trying to accomplish with Array#sort at the end of your query, but you could always do something like:
#users_with_posts_in_selected = User.find(#selected_posts.map(&:user_id).uniq).order('username DESC')
I don't understand your question but you can pass an array to the where method like this:
where(:id => #selected_posts.map(&:id))
and it will create a SQL query like WHERE id IN (1,2,3,4)
By virtue of your associations your selected posts already have the users:
#selected_posts = Posts.where("posts.category =?", "Baseball")
#users = #selected_posts.collect(&:user);
You'll probably want to remove duplicate users from #users.
I want to grab the most recent entry from a table. If I was just using sql, you could do
Select top 1 * from table ORDER BY EntryDate DESC
I'd like to know if there is a good active record way of doing this.
I could do something like:
table.find(:order => 'EntryDate DESC').first
But it seems like that would grab the entire result set, and then use ruby to select the first result. I'd like ActiveRecord to create sql that only brings across one result.
You need something like:
Model.first(:order => 'EntryDate DESC')
which is shorthand for
Model.find(:first, :order => 'EntryDate DESC')
Take a look at the documentation for first and find for details.
The Rails documentation seems to be pretty subjective in this instance. Note that .first is the same as find(:first, blah...)
From:http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002263
"Find first - This will return the first record matched by the options used. These options can either be specific conditions or merely an order. If no record can be matched, nil is returned. Use Model.find(:first, *args) or its shortcut Model.first(*args)."
Digging into the ActiveRecord code, at line 1533 of base.rb (as of 9/5/2009), we find:
def find_initial(options)
options.update(:limit => 1)
find_every(options).first
end
This calls find_every which has the following definition:
def find_every(options)
include_associations = merge_includes(scope(:find, :include), options[:include])
if include_associations.any? && references_eager_loaded_tables?(options)
records = find_with_associations(options)
else
records = find_by_sql(construct_finder_sql(options))
if include_associations.any?
preload_associations(records, include_associations)
end
end
records.each { |record| record.readonly! } if options[:readonly]
records
end
Since it's doing a records.each, I'm not sure if the :limit is just limiting how many records it's returning after the query is run, but it sure looks that way (without digging any further on my own). Seems you should probably just use raw SQL if you're worried about the performance hit on this.
Could just use find_by_sql http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002267
table.find_by_sql "Select top 1 * from table ORDER BY EntryDate DESC"