This project is archived and is in readonly mode.
Improve conditions hash to support complex queries
-
Nathan Esquenazi
- Title changed from Improve conditions hash to support more complex queries to Improve conditions hash to support complex queries
-
nerdrew
Another approach would be:
Person.find(:all, :conditions => { :age.gt => 30, :name.like => "%a%", :or => { :place => 'Boca', :occupation => 'Chef'} }) #=> people.`age` > 30 AND people.`name` LIKE "%a%" AND (people.place = "Boca" OR people.occupation = "Chef")This allows the construction of queries with AND's and OR's used.
-
nerdrew
I would really like to see something like this in active record.
-
Nathan Esquenazi
Thanks for your feedback nerdrew. I actually do like that syntax. I was trying to think of a way to be able to combine AND / OR into a single hash. I think you touched on a good way to do that.
I want to go in baby steps though in hopes of even getting the enhanced AND hash into core and go from there. This is one of those features I want to see on edge rails as being included in rails 2.2.
I have created the plugin here: http://github.com/xgamerx/condit...
But again, this is something I would prefer to see baked into activerecord.
-
nerdrew
Another thing I'd like to see in active record conditions hashes is the ability to use conditions on included tables. Also I'd like these tables to be automatically included. I understand that it needs to be kept simple in order to be included into edge rails, but while I'm wishing away, I figured I'd add it.
Person.find(:all, :conditions => {:name => 'Bob', :home => {:state => 'CA'}} #=> SELECT * FROM people left outer join homes on person.id = homes.person_id WHERE person.name = "Bob" AND homes.state = "CA"I know the SQL isn't exactly the same as AR output, but it gives the idea.
-
Pratik
- State changed from new to wontfix
- Tag set to activerecord, conditions, enhancement
Please discuss this on the mailing list. There might be some parallel efforts for doing the same. IIRC, ARel does this as well.
-
Mihai Alexandru Bîrsan
What @nerdrew said can be easily achieved by looking at associations (like what :include does). Also, the query should only select the model being asked for, unless otherwise specified.
@@@ruby Person.find(:all, :conditions => {:name => 'Bob', :home => {:state => 'CA'}}
=> SELECT person.* FROM people left outer join homes on person.id = homes.person_id WHERE person.name = "Bob" AND homes.state = "CA"
Perhaps :joins => :homes could be specified to take `SELECT *` instead of `SELECT person.*`. -
nerdrew
I've already done something so I can use the conditions as I've described them. I've attached the relevant part of my plugin for this.
-
Mihai Alexandru Bîrsan
@nerdrew I can't see your attachment... Did it go through? :)
-
nerdrew
It shows up on the right side above the list of people watching this ticket for me. If it didn't go through I can resend it. ar_conditions.rb

