Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow conditions on multiple tables to be specified using hash.
Examples: User.all :joins => :items, :conditions => { :age => 10, :items => { :color => 'black' } } Item.first :conditions => { :items => { :color => 'red' } } Note : Hash key in :conditions is referring to the actual table name or the alias defined in query.
- Loading branch information
Showing
3 changed files
with
37 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cd994ef
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi,
Cool. This is very useful because it allows me to not have to worry about the name of the table when I join to a table and then back again, eg. like finding another book on the same bookshelf:
Books.all(
:joins => {:bookshelf => :books},
:conditions => {:bookshelves => {:books => {:title => ‘Another Book on the Same Shelf’}}}
)
Without having to worry about the name of the second book table in the SQL like I used to. So thanks!
I have 2 suggestions, though.
- I don’t seem to find that it works with :include, only :joins. Is there any reason for this?
- To me it makes more intuitive sense that the symbols are the same as the :joins, so it would be :conditions => {:bookshelf => instead of :conditions => :bookshelves as is currently the case.
But overall, very useful.
ben
cd994ef
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, sorry to be a pain, but the above seems to actually illustrate a bug. Say I’m trying to get the names of all the books on a particular shelf (which are book1 and book2), and all I have is the name of one of the books (book1).
Using your method, I would
I end up getting
I suppose this is a bug, but I’m too lazy to file it on lighthouse.
cd994ef
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bug report:
http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/710-condition-hashes-through-joins-gives-fails-when-circular-references-are-used
cd994ef
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Pratik