#15 Fun with Find Conditions
Apr 06, 2007 | 4 minutes | Active Record
You can pass more than simple strings to find conditions. Arrays, ranges, and nil values can be passed as well. In this episode you will see the tricks involved with passing these odd objects to find conditions. (Update: audio fixed).
Any idea how to specify exclusion criteria? For example, what is the syntax for excluding records with priority 1 & 3 using the hash for input.
@Bob, I don't think that's possible. You'll have to use a string for the conditions.
Thanks Ryan, that's what I thought, so I should specify "priority NOT IN (?)", in the string.
Hi, how do you combine this with eagerLoading?
Task.find(:all, :include => projects, :conditions => ["tasks.complete=? and tasks.priority = ?", false, nil])
Does this work?
tHnx
What the best way to do something like this:
Task.find(:all, :conditions => {:start.year => 2008})
without user database functions...
It's posssible?
Very good Rails screencasts!
Thank's
Thanks for the screencast!
How can I do this in smarter way?
(OR) conditions??
Something.find(:all, :conditions => ["user_id=? OR is_public=?", current_user.id, true])
Basically, I want to search for something that is belong to user, or public..
can i ask?
how about if u want to do something like this....
id_files = 123,245,567
find(:all, :conditions => ["id in (?)", [id_files]])
About doing that for nested objects, if the query has :include or :joins.
i have tried to do this and it worked 'test_nested.test_id'.to_sym => 1
Any problems that may come from that?
Thanks for the screen screen casts, which i really the lighthoue for any new comer.
My following code not working, please guide, thanks.
@params['body'].each do |contact|
@c = Contact.find(:all, :conditions=> ["contact_id = ?", contact["contact_id"]])
if @c.count > 0
# update record
else
# insert record
end
end
find_all_by_complete.... is not working for me..
can u please tell me what might be the problem...
This episode has been updated for Rails 5 as a blog post. Rails 5 ActiveRecord where, find_by, or and Eager Loading
This is one of those episodes where it's been long enough that @bparanj's updates are really handy to see where 'find_by' and "where' are at as of rails 5.... Hadn't heard of 'find_by_column' handling range functionality!
gunawangsa