Nikolai Teleguine
2010-Oct-15 20:01 UTC
ActiveRecord - how to search by association value?
--- class Child < Entity belongs_to :parent, :foreign_key => ''parent_id'' end class Parent < Entity end class Entity < ActiveRecord::Base end --- p = Parent.find_by_name( "Michael" ) How do I find children of this parent, given that I have the identity of the parent? Can I do it without referring to parent entity attributes/values directly ? Something like Child.find :parent => p Rails version is 2.1.0, if that matters. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Erol Fornoles
2010-Oct-16 09:50 UTC
Re: ActiveRecord - how to search by association value?
class Parent < Entity has_many :children end p = Parent.find_by_name("Michael") p.children # Returns all children of the parent HTH On Sat, Oct 16, 2010 at 4:01 AM, Nikolai Teleguine <nteleguine-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> --- > class Child < Entity > belongs_to :parent, :foreign_key => ''parent_id'' > end > > class Parent < Entity > end > > class Entity < ActiveRecord::Base > end > --- > > p = Parent.find_by_name( "Michael" ) > > How do I find children of this parent, given that I have the identity > of the parent? > Can I do it without referring to parent entity attributes/values > directly ? Something like > > Child.find :parent => p > > Rails version is 2.1.0, if that matters. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Erol M. Fornoles http://erolfornoles.posterous.com http://github.com/Erol http://twitter.com/erolfornoles http://ph.linkedin.com/in/erolfornoles -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Nikolai Teleguine
2010-Oct-18 14:35 UTC
Re: ActiveRecord - how to search by association value?
Erol, Thank you for the reply. This will work, but is there a way to do this search without adding another association? I am just trying to learn the capabilities of ActiveRecord. Let me re-state the problem: I either already have the parent object, or know enough of it''s attributes to find the parent. But the goal is to find the child, using only its ''parent'' association. Child.find (:name=>"Timmy", parent => knownParent ) - or - Child.find (:name=>"Timmy", parent.name => "Michael") On Sat, Oct 16, 2010 at 5:50 AM, Erol Fornoles <erol.fornoles-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> class Parent < Entity > has_many :children > end > p = Parent.find_by_name("Michael") > p.children # Returns all children of the parent > HTH > > On Sat, Oct 16, 2010 at 4:01 AM, Nikolai Teleguine <nteleguine-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> >> --- >> class Child < Entity >> belongs_to :parent, :foreign_key => ''parent_id'' >> end >> >> class Parent < Entity >> end >> >> class Entity < ActiveRecord::Base >> end >> --- >> >> p = Parent.find_by_name( "Michael" ) >> >> How do I find children of this parent, given that I have the identity >> of the parent? >> Can I do it without referring to parent entity attributes/values >> directly ? Something like >> >> Child.find :parent => p >> >> Rails version is 2.1.0, if that matters. >> >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > > > -- > Erol M. Fornoles > http://erolfornoles.posterous.com > http://github.com/Erol > http://twitter.com/erolfornoles > http://ph.linkedin.com/in/erolfornoles > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Erol Fornoles
2010-Oct-18 15:40 UTC
Re: ActiveRecord - how to search by association value?
You can use dynamic finders: Child.find_by_parent_id_and_name(parent.id, "Timmy") But then again, if you already have the parent object, the common practice is to setup the children association and get the child using it: parent.children.find_by_name("Timmy") Which IMO is more readable than the first example. On Mon, Oct 18, 2010 at 10:35 PM, Nikolai Teleguine <nteleguine-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> Erol, > > Thank you for the reply. > > This will work, but is there a way to do this search without adding > another association? > I am just trying to learn the capabilities of ActiveRecord. > > Let me re-state the problem: I either already have the parent object, > or know enough of it''s attributes to find the parent. > But the goal is to find the child, using only its ''parent'' association. > > Child.find (:name=>"Timmy", parent => knownParent ) > - or - > Child.find (:name=>"Timmy", parent.name => "Michael") > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Erol M. Fornoles http://erolfornoles.posterous.com http://github.com/Erol http://twitter.com/erolfornoles http://ph.linkedin.com/in/erolfornoles -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Nikolai Teleguine
2010-Oct-18 22:40 UTC
Re: ActiveRecord - how to search by association value?
Erol, Thanks, it did not occur to me to try this kind of finder. The second option also works, especially "common practice" part - learning Rails idioms here. I initially assumed ActiveRecord would be able to build a query based on known PK and FK attributes of the association without explicit reference of parent''s id, which would be, in my opinion, more OO Nikolai On Mon, Oct 18, 2010 at 11:40 AM, Erol Fornoles <erol.fornoles-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You can use dynamic finders: > Child.find_by_parent_id_and_name(parent.id, "Timmy") > > But then again, if you already have the parent object, the common practice > is to setup the children association and get the child using it: > parent.children.find_by_name("Timmy") > Which IMO is more readable than the first example. > On Mon, Oct 18, 2010 at 10:35 PM, Nikolai Teleguine <nteleguine-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> >> Erol, >> >> Thank you for the reply. >> >> This will work, but is there a way to do this search without adding >> another association? >> I am just trying to learn the capabilities of ActiveRecord. >> >> Let me re-state the problem: I either already have the parent object, >> or know enough of it''s attributes to find the parent. >> But the goal is to find the child, using only its ''parent'' association. >> >> Child.find (:name=>"Timmy", parent => knownParent ) >> - or - >> Child.find (:name=>"Timmy", parent.name => "Michael") >> >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > > > -- > Erol M. Fornoles > http://erolfornoles.posterous.com > http://github.com/Erol > http://twitter.com/erolfornoles > http://ph.linkedin.com/in/erolfornoles > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.