Walter Wilfinger
2008-May-14 21:01 UTC
Get parents via a condition on a child? (ActiveRecord)
I come from a PHP background where I worked without an ORM. I''m having some trouble converting what I know will work in raw SQL into ActiveRecord models. I have the following two models (not my actual models, just as a generic example): class Topic < ActiveRecord::Base has_many :posts ... end class Post < ActiveRecord::Base belongs_to :topic attr_accessible :body ... end I want to work with the collection of Topics that have Posts with a certain value of :body. That is, "All topics that contain posts whose body is equal to ''example''" If I was working with straight SQL I would do this SELECT topics.* FROM topics JOIN posts ON topics.id = posts.topic_id WHERE posts.body = "example" In ActiveRecord I can get the list of Posts without a problem. Post.find(:all, :conditions => {:body => "example"}) I think this is equivalent to the above Post.find_all_by_body("example") What I can''t figure out is how to get the collection of Topics. This doesn''t work because Topics don''t have :body''s Topic.find(:all, :include => :posts, :conditions => {:body => "example"}) I''ve looked at the generated SQL and because the :posts table is aliased I don''t think I can come up with SQL fragment that would consistently work. I can see how I may be able to first get the collection of Posts and then in another call to #find get the Topics I need, but that seems too roundabout. What''s the best way to do this with ActiveRecord? Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
maestro777
2008-May-14 22:56 UTC
Re: Get parents via a condition on a child? (ActiveRecord)
In my limited understanding, I think if you managed to pull out the collection of Post, you have access to all Topic that are linked to that collection of Post. I don''t know if this will work, I think the following example will print out the Topic name of each of the post that has body = example. allpost = Post.find(:all, :conditions => {:body => "example"}) allpost.each do | onepost | puts onepost.topic.name end On May 14, 2:01 pm, Walter Wilfinger <rails-mailing-l...@andreas- s.net> wrote:> I come from a PHP background where I worked without an ORM. I''m having > some trouble converting what I know will work in raw SQL into > ActiveRecord models. > > I have the following two models (not my actual models, just as a generic > example): > > class Topic < ActiveRecord::Base > has_many :posts > ... > end > > class Post < ActiveRecord::Base > belongs_to :topic > attr_accessible :body > ... > end > > I want to work with the collection of Topics that have Posts with a > certain value of :body. That is, "All topics that contain posts whose > body is equal to ''example''" > > If I was working with straight SQL I would do this > > SELECT topics.* FROM > topics JOIN posts ON topics.id = posts.topic_id > WHERE > posts.body = "example" > > In ActiveRecord I can get the list of Posts without a problem. > > Post.find(:all, :conditions => {:body => "example"}) > > I think this is equivalent to the above > > Post.find_all_by_body("example") > > What I can''t figure out is how to get the collection of Topics. > > This doesn''t work because Topics don''t have :body''s > > Topic.find(:all, :include => :posts, :conditions => {:body => > "example"}) > > I''ve looked at the generated SQL and because the :posts table is aliased > I don''t think I can come up with SQL fragment that would consistently > work. > > I can see how I may be able to first get the collection of Posts and > then in another call to #find get the Topics I need, but that seems too > roundabout. > > What''s the best way to do this with ActiveRecord? > > Thanks! > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg (Radar)
2008-May-14 23:31 UTC
Re: Get parents via a condition on a child? (ActiveRecord)
So you want to find all the topics where a post in that topic has the body of "example"? Topic.find(:all, :include => :posts, :conditions => ["posts.body ?",params[:query]]) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---