Forum: Ruby on Rails ambiguous nested conditions on find

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
linojon (Guest)
on 2008-11-05 06:49
(Received via mailing list)
Hi, i'm trying to figure out how to do a query with an ambiguous
condition

Lets say Comment belongs_to :article and :user,
and Article :belongs_to :user

How can I find the comments by one user on articles by another user
in one query?

This doesnt work, but is kind of the idea:

     Comment.find :all, :conditions => {'users.name' =>
'Cccc', :article => { 'users.name' => 'Aaaa' } }, :include => [:user,
{ :article => :user } ]

linoj
Frederick C. (Guest)
on 2008-11-05 13:21
(Received via mailing list)
On 5 Nov 2008, at 04:48, linojon wrote:

> This doesnt work, but is kind of the idea:
>
>     Comment.find :all, :conditions => {'users.name' =>
> 'Cccc', :article => { 'users.name' => 'Aaaa' } }, :include => [:user,
> { :article => :user } ]
>
The table will be aliased, so your conditions need to be something
like :conditions => {'users.name' => 'Cccc', 'aliased_users.name' =>
'Aaaa'}, :include (or :joins) =>...
It won't actually be aliased_users, however I believe the naming
scheme for the aliases is deterministic, so just have a look at the
query generated to see what table aliases are being generated

Fred
This topic is locked and can not be replied to.