Hi, Just new to Rails,, and i have some questions : How can i use ActiveRecord associations to retrieve all columns and rows including the columns and rows in the referenced table.? *SELECT parents.id, parents.parent_name, kids.id, kids.parent_id, kids.kid_name FROM parents left join kids on parents.id = kids.parent_id * In PHP in can retrieve all rows returned by this query from all joined tables using mysql_fetch_array() and the output is *parents.id, parents.parent_name, kids.id, kids.parent_id, kids.kid_name* Is there an equivalent of this in RoR? Here is the AR Associations that i want to retrieve all columns including those belonging to the referenced table *Code: * = primary key + = foreign key Table : parents *id parent_name Table : kids *id +parent_id kid_name class Parent < ActiveRecord::Base has_many :kids end class Kid < ActiveRecord::Base belongs_to :parents end* Thanks! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Jun 26, 2:00 am, "ryan bayona" <ryanbay...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > Just new to Rails,, and i have some questions :Welcome!> How can i use ActiveRecord associations to retrieve all columns and rows > including the columns and rows in the referenced table.? > > *SELECT parents.id, parents.parent_name, kids.id, kids.parent_id, > kids.kid_name > FROM parents left join kids on parents.id = kids.parent_id > * > In PHP in can retrieve all rows returned by this query from all joined > tables using mysql_fetch_array() and the output is > *parents.id, parents.parent_name, kids.id, kids.parent_id, kids.kid_name* > > Is there an equivalent of this in RoR?This is one of the changes in thinking when we switch to Rails. The belongs_to and has_many lets us not worry about the underlying queries too much. For example, if the goal is to get a list of kids given a parent, then it''s this easy: parent = Parent.find(1) # 1 is the parent_id, use whatever you want parent.kids.size => returns the number of kids parent.kids.first.kid_name # returns the name of the first kid Another example: kids = Parent.find(1).kids # get all kids for the parent names = kids.collect { |kid| kid.name } # names is now an array of names You may also want to take a look at "Rails for PHP Programmers" by Pragmatic Press. Hope this helps? Jeff softiesonrails.com purpleworkshops.com> Here is the AR Associations that i want to retrieve all columns including > those belonging to the referenced table > > *Code: > > * = primary key > + = foreign key > > Table : parents > *id > parent_name > > Table : kids > *id > +parent_id > kid_name > > class Parent < ActiveRecord::Base > has_many :kids > end > > class Kid < ActiveRecord::Base > belongs_to :parents > end* > > Thanks!--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Also try parent = Parent.find(1, :include => :kids) Then subsequent calls like Jeff mentions won''t actually go to the DB (they are already loaded)... On Jun 26, 12:20 pm, Jeff <cohen.j...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jun 26, 2:00 am, "ryan bayona" <ryanbay...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > Just new to Rails,, and i have some questions : > > Welcome! > > > How can i use ActiveRecord associations to retrieve all columns and rows > > including the columns and rows in the referenced table.? > > > *SELECT parents.id, parents.parent_name, kids.id, kids.parent_id, > > kids.kid_name > > FROM parents left join kids on parents.id = kids.parent_id > > * > > In PHP in can retrieve all rows returned by this query from all joined > > tables using mysql_fetch_array() and the output is > > *parents.id, parents.parent_name, kids.id, kids.parent_id, kids.kid_name* > > > Is there an equivalent of this in RoR? > > This is one of the changes in thinking when we switch to Rails. The > belongs_to and has_many lets us not worry about the underlying queries > too much. > > For example, if the goal is to get a list of kids given a parent, then > it''s this easy: > > parent = Parent.find(1) # 1 is the parent_id, use whatever you want > parent.kids.size => returns the number of kids > parent.kids.first.kid_name # returns the name of the first kid > > Another example: > > kids = Parent.find(1).kids # get all kids for the parent > names = kids.collect { |kid| kid.name } # names is now an array of > names > > You may also want to take a look at "Rails for PHP Programmers" by > Pragmatic Press. > > Hope this helps? > > Jeff > softiesonrails.com > purpleworkshops.com > > > Here is the AR Associations that i want to retrieve all columns including > > those belonging to the referenced table > > > *Code: > > > * = primary key > > + = foreign key > > > Table : parents > > *id > > parent_name > > > Table : kids > > *id > > +parent_id > > kid_name > > > class Parent < ActiveRecord::Base > > has_many :kids > > end > > > class Kid < ActiveRecord::Base > > belongs_to :parents > > end* > > > Thanks!--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ryanbayona-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jun-26 22:14 UTC
Re: ActiveRecord get all columns including those belonging
Im going to try your suggestions. I will also find that book you mentioned.., Thank you so much! On Jun 27, 1:23 am, Harold <harold.gime...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Also try > > parent = Parent.find(1, :include => :kids) > > Then subsequent calls like Jeff mentions won''t actually go to the DB > (they are already loaded)... > > On Jun 26, 12:20 pm, Jeff <cohen.j...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Jun 26, 2:00 am, "ryan bayona" <ryanbay...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hi, > > > Just new to Rails,, and i have some questions : > > > Welcome! > > > > How can i use ActiveRecord associations to retrieve all columns and rows > > > including the columns and rows in the referenced table.? > > > > *SELECT parents.id, parents.parent_name, kids.id, kids.parent_id, > > > kids.kid_name > > > FROM parents left join kids on parents.id = kids.parent_id > > > * > > > In PHP in can retrieve all rows returned by this query from all joined > > > tables using mysql_fetch_array() and the output is > > > *parents.id, parents.parent_name, kids.id, kids.parent_id, kids.kid_name* > > > > Is there an equivalent of this in RoR? > > > This is one of the changes in thinking when we switch to Rails. The > > belongs_to and has_many lets us not worry about the underlying queries > > too much. > > > For example, if the goal is to get a list of kids given a parent, then > > it''s this easy: > > > parent = Parent.find(1) # 1 is the parent_id, use whatever you want > > parent.kids.size => returns the number of kids > > parent.kids.first.kid_name # returns the name of the first kid > > > Another example: > > > kids = Parent.find(1).kids # get all kids for the parent > > names = kids.collect { |kid| kid.name } # names is now an array of > > names > > > You may also want to take a look at "Rails for PHP Programmers" by > > Pragmatic Press. > > > Hope this helps? > > > Jeff > > softiesonrails.com > > purpleworkshops.com > > > > Here is the AR Associations that i want to retrieve all columns including > > > those belonging to the referenced table > > > > *Code: > > > > * = primary key > > > + = foreign key > > > > Table : parents > > > *id > > > parent_name > > > > Table : kids > > > *id > > > +parent_id > > > kid_name > > > > class Parent < ActiveRecord::Base > > > has_many :kids > > > end > > > > class Kid < ActiveRecord::Base > > > belongs_to :parents > > > end* > > > > Thanks!--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---