neerolyte
2009-Mar-12 06:05 UTC
Beginner: what to do after adding an association in a model?
I''ve looked at a bunch of tutorials and don''t really understand what is supposed to happen after I add an association (belongs_to, has_many etc) to both sides of a relationship. A lot of the tutorials just add the associations and move on as if that''s it, but when I add the association nothing has changed in the db (which makes sense because at this point I''ve only changed code). Is there a command I''m supposed to run to look at all the relationships and add tables/columns for relationships that are missing them? or am I supposed to manually create the table? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Mar-12 09:32 UTC
Re: Beginner: what to do after adding an association in a model?
On Mar 12, 6:05 am, neerolyte <neerol...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve looked at a bunch of tutorials and don''t really understand what > is supposed to happen after I add an association (belongs_to, has_many > etc) to both sides of a relationship. > > A lot of the tutorials just add the associations and move on as if > that''s it, but when I add the association nothing has changed in the > db (which makes sense because at this point I''ve only changed code). > Is there a command I''m supposed to run to look at all the > relationships and add tables/columns for relationships that are > missing them? or am I supposed to manually create the table?You''re suppose to use migrations to add columns or create tables (see http://guides.rubyonrails.org/migrations.html ) Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rabia Akhtar
2009-Mar-12 09:33 UTC
Re: Beginner: what to do after adding an association in a model?
I dont think so you need to do any thing manually with the db.. Just use the association by using dot operator. Regards Rabia On Thu, Mar 12, 2009 at 11:05 AM, neerolyte <neerolyte-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I''ve looked at a bunch of tutorials and don''t really understand what > is supposed to happen after I add an association (belongs_to, has_many > etc) to both sides of a relationship. > > A lot of the tutorials just add the associations and move on as if > that''s it, but when I add the association nothing has changed in the > db (which makes sense because at this point I''ve only changed code). > Is there a command I''m supposed to run to look at all the > relationships and add tables/columns for relationships that are > missing them? or am I supposed to manually create the table? > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
David Schoen
2009-Mar-12 10:00 UTC
Re: Beginner: what to do after adding an association in a model?
Does that mean I have to write a custom migration for it, the only automated migrations I can find are for adding/removing columns (AddXXXtoYYY). Is there something like AddHasAndBelongsToManyModelXToModelY? Dave. 2009/3/12 Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> On Mar 12, 6:05 am, neerolyte <neerol...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> I''ve looked at a bunch of tutorials and don''t really understand what >> is supposed to happen after I add an association (belongs_to, has_many >> etc) to both sides of a relationship. >> >> A lot of the tutorials just add the associations and move on as if >> that''s it, but when I add the association nothing has changed in the >> db (which makes sense because at this point I''ve only changed code). >> Is there a command I''m supposed to run to look at all the >> relationships and add tables/columns for relationships that are >> missing them? or am I supposed to manually create the table? > > You''re suppose to use migrations to add columns or create tables (see > http://guides.rubyonrails.org/migrations.html ) > > Fred--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ar Chron
2009-Mar-12 13:22 UTC
Re: Beginner: what to do after adding an association in a model?
The associations you create in a model have to be backed up by the appropriate fields in the DB. For example: class Person has_many :addresses class Address belongs_to :person should be a model representation of the relationship inherent in the database (the two really go hand-in-hand). if: Table people id:integer first_name:string last_name:string Table address id:integer person_id:integer line1:string line2:string city:string state:string postal_code:string has_many :addresses tells Rails that for a given person, it can use that person id field to retrieve address records (those whose person_id matches the current person id value). Similarly, from an address, Rails can get back to the person record by following the person_id on the address. -- 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 -~----------~----~----~----~------~----~------~--~---
rroman81
2009-Mar-12 20:34 UTC
Re: Beginner: what to do after adding an association in a model?
So building on that example, you should add a column person_id to the address table. Rails will infer the foreign key based on rails model names. U can also specify the actual column by passing :foreign_key into belongs_to macro. Roman On Mar 12, 6:22 am, Ar Chron <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> The associations you create in a model have to be backed up by the > appropriate fields in the DB. > > For example: > > class Person > has_many :addresses > > class Address > belongs_to :person > > should be a model representation of the relationship inherent in the > database (the two really go hand-in-hand). > > if: > > Table people > id:integer > first_name:string > last_name:string > > Table address > id:integer > person_id:integer > line1:string > line2:string > city:string > state:string > postal_code:string > > has_many :addresses tells Rails that for a given person, it can use that > person id field to retrieve address records (those whose person_id > matches the current person id value). Similarly, from an address, Rails > can get back to the person record by following the person_id on the > address. > > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---