Hi, there: I made two models - "task" and "comment" in my app without any connections each other, but now I wanna a "one to many" between them, saying one task has many comments, one comments has to belong to one task. Therefore, I add "belongs_to :task" and "has_many :comments" in each model respectively. The question is: 1> I dont think the two tables in the database will add the foreign_key column inside automatically after I said "belongs_to & has" in models. Am I right? At least I didnt find it by investigating database tables. 2> what should I do if I dont wanna use SQL or DDL to create foreign_key cloumn, is the "migrate" able to help me? Thanks a lot! Myst --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
myst_tt wrote:> Hi, there: > I made two models - "task" and "comment" in my app without any > connections each other, but now I wanna a "one to many" between them, > saying one task has many comments, one comments has to belong to one > task. > Therefore, I add "belongs_to :task" and "has_many :comments" in each > model respectively. >thats good and how it should be done> The question is: > 1> I dont think the two tables in the database will add the > foreign_key column inside automatically after I said "belongs_to & > has" in models. Am I right? At least I didnt find it by investigating > database tables.you need to add the foreign keys yourself. rails is not doing it for you. if you stick to the convention that the foreign key is named id_classname then it will be picked up by the relationshsip automatically .. otherwise you will need to specify it explicitely in the code> 2> what should I do if I dont wanna use SQL or DDL to create > foreign_key cloumn, is the "migrate" able to help me?yep, use a migration to add the column you need> > Thanks a lot! > Myst-- 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 -~----------~----~----~----~------~----~------~--~---
Thank you! On 1月6日, 下午11时21分, Michal Gabrukiewicz <rails-mailing-l...@andreas- s.net> wrote:> myst_tt wrote: > > Hi, there: > > I made two models - "task" and "comment" in my app without any > > connections each other, but now I wanna a "one to many" between them, > > saying one task has many comments, one comments has to belong to one > > task. > > Therefore, I add "belongs_to :task" and "has_many :comments" in each > > model respectively. > > thats good and how it should be done > > > The question is: > > 1> I dont think the two tables in the database will add the > > foreign_key column inside automatically after I said "belongs_to & > > has" in models. Am I right? At least I didnt find it by investigating > > database tables. > > you need to add the foreign keys yourself. rails is not doing it for > you. if you stick to the convention that the foreign key is named > id_classname then it will be picked up by the relationshsip > automatically .. otherwise you will need to specify it explicitely in > the code > > > 2> what should I do if I dont wanna use SQL or DDL to create > > foreign_key cloumn, is the "migrate" able to help me? > > yep, use a migration to add the column you need > > > > > Thanks a lot! > > Myst > > -- > 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 -~----------~----~----~----~------~----~------~--~---