An Account has_many Websites which in turn has_many WebsiteDomains Now I can of course do this: @domains = Account.find(1).websites.find(1).website_domains.find(:all) To get all the domains for Account with id 1 and Website with id 1. I would like to do something like this though: @domains = Account.find(1).websites.find(:all).website_domains.find(:all) IE, get ALL domains for all websites in Account 1 The code above doesn''t work because of the multiple find(:all) I could do it using :joins and :conditions I guess but then I lose other useful stuff like attributes matching column names in the database. Any ideas? -- Posted via http://www.ruby-forum.com/.
@domains = Domain.find(:all, :conditions => ["websites.id = ? AND websites.account_id = ?", website_id, acount_id], :include => "website") Should do it. May need to be tweaked a little. -Nick On 1/17/06, Per Djurner <per.djurner@gmail.com> wrote:> > An Account has_many Websites which in turn has_many WebsiteDomains > > Now I can of course do this: > @domains = Account.find(1).websites.find(1).website_domains.find(:all) > To get all the domains for Account with id 1 and Website with id 1. > > I would like to do something like this though: > @domains > Account.find(1).websites.find(:all).website_domains.find(:all) > IE, get ALL domains for all websites in Account 1 > The code above doesn''t work because of the multiple find(:all) > > I could do it using :joins and :conditions I guess but then I lose other > useful stuff like attributes matching column names in the database. > > Any ideas? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060117/33ffdcea/attachment.html
Rob Biedenharn
2006-Jan-17 15:22 UTC
[Rails] using "find" when you have 2 has_many relations
<warning type="noob answer"> @account = Account.find(1) @domains = [] @account.websites.each { |w| @domains << w.website_domains } @domains.flatten! </warning> I hope this is on the right track. It just seems like you want to let ActiveRecord do the work since you''ve gone to the trouble to define the relations. -Rob At 1/17/2006 10:08 AM, Nick Stuart wrote:>@domains = Domain.find(:all, :conditions => >["<http://websites.id>websites.id = ? AND websites.account_id = ?", >website_id, acount_id], :include => "website") > >Should do it. May need to be tweaked a little. > >-Nick > >On 1/17/06, Per Djurner ><<mailto:per.djurner@gmail.com>per.djurner@gmail.com> wrote: >An Account has_many Websites which in turn has_many WebsiteDomains > >Now I can of course do this: >@domains = Account.find(1).websites.find(1).website_domains.find(:all) >To get all the domains for Account with id 1 and Website with id 1. > >I would like to do something like this though: >@domains >Account.find(1).websites.find(:all).website_domains.find(:all) >IE, get ALL domains for all websites in Account 1 >The code above doesn''t work because of the multiple find(:all) > >I could do it using :joins and :conditions I guess but then I lose other >useful stuff like attributes matching column names in the database. > >Any ideas? > >-- >Posted via <http://www.ruby-forum.com/>http://www.ruby-forum.com/.-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060117/1de0d4ce/attachment.html
Oops, I forgot that I actually had one more relation that has to go in there. Watcher belongs_to WebsiteDomain. So what I really would like to do is something like this: @watcher = Watcher.find(:all, :conditions => [websites.account_id = ?", acount_id], :include => ["website_domain", "website"]) This does not work however and I just found this on the RoR API site: "It?s currently not possible to use eager loading on multiple associations from the same table." Is that why I can not do this? If so, any work-arounds for it? -- Posted via http://www.ruby-forum.com/.
Nick Stuart
2006-Jan-17 17:52 UTC
[Rails] Re: using "find" when you have 2 has_many relations
I guess it comes down to what you are looking for in the final results. Whats the end goal? A list of domains per watcher? per website? per account? Need some more info to be able to refine it a bit more. -Nick On 1/17/06, Per Dj <per.djurner@gmail.com> wrote:> > Oops, I forgot that I actually had one more relation that has to go in > there. > Watcher belongs_to WebsiteDomain. > > So what I really would like to do is something like this: > > @watcher = Watcher.find(:all, :conditions => [websites.account_id = ?", > acount_id], :include => ["website_domain", "website"]) > > This does not work however and I just found this on the RoR API site: > "It''s currently not possible to use eager loading on multiple > associations from the same table." > > Is that why I can not do this? > > If so, any work-arounds for it? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060117/22f5c701/attachment.html
Per Dj
2006-Jan-17 18:02 UTC
[Rails] Re: Re: using "find" when you have 2 has_many relations
The goal is to get all Watchers for one Account. So my main problem is that since I have so many relations (Account --> Websites --> WebsiteDomains --> Watchers) in between those two I''m not sure how to code it in any elegant way. Thanks for your help, really appreciate it! -- Posted via http://www.ruby-forum.com/.
Jeremy Evans
2006-Jan-17 19:04 UTC
[Rails] Re: using "find" when you have 2 has_many relations
On 1/17/06, Per Dj <per.djurner@gmail.com> wrote:> Oops, I forgot that I actually had one more relation that has to go in > there. > Watcher belongs_to WebsiteDomain. > > So what I really would like to do is something like this: > > @watcher = Watcher.find(:all, :conditions => [websites.account_id = ?", > acount_id], :include => ["website_domain", "website"]) > > This does not work however and I just found this on the RoR API site: > "It''s currently not possible to use eager loading on multiple > associations from the same table." > > Is that why I can not do this?Because Rails wasn''t designed with it in mind (it doesn''t alias tables in the SQL), and patches to implement it have been rejected (see http://dev.rubyonrails.org/ticket/1562).> If so, any work-arounds for it?There''s the Allow Multiple Associations Same Table plugin: http://wiki.rubyonrails.org/rails/pages/Allow+Multiple+Associations+Same+Table+Plugin, which is a conversion of the latest patch in the ticket mentioned above.
Per Dj
2006-Jan-17 21:12 UTC
[Rails] Re: Re: using "find" when you have 2 has_many relations
That''s a surprise :O Thanks for the info. So if I want to avoid plugins and engines etc then my only choice is to raw sql with find_by_sql ? -- Posted via http://www.ruby-forum.com/.
Nick Stuart
2006-Jan-18 02:13 UTC
[Rails] Re: Re: using "find" when you have 2 has_many relations
Looks to be the easiest way yes, but its not to painful is it? Watcher.find_by_sql(["SELECT w.* FROM watchers w LEFT JOIN website_domains ON website_domains.id = w.website_domain_id LEFT JOIN websites ON websites.id = website_domains.website_id WHERE websites.account_id = ?", account_id]) Should do it if I got the table/column names right... On 1/17/06, Per Dj <per.djurner@gmail.com> wrote:> That''s a surprise :O > Thanks for the info. > > So if I want to avoid plugins and engines etc then my only choice is to > raw sql with find_by_sql ? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >