I am trying to allow a user to answer some questions and then save those questions but I am unsure if I am getting the layout correct. Here is what I have so far: There is a questions table holding the questions to ask There is an answers tables which holds the answers to the questions asked of a user. Then there is of course the Users table which hold a variety of user info I figure my migrations would look something like: Questions: has_many :answers Answers: belong_to :user, belongs_to :question User has_many :answers so my table would look like: User: id, username, . . . Questions: id, question Answer: id, user_id, question_id, answer Where I am a little confused is if the belongs_to :question should rather be a has_one :question and would that change the schema at all? Also, can anyone point me to some good online tutorials involving Model Relationships, thanks, -S -- 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 -~----------~----~----~----~------~----~------~--~---
When you "belong_to" it means that the foreing key is at your side, you belong to the question ''cos you wouldn''t be able to exist without it, there''s no reason to have a question without a user or an answer. A has_one means that you have of of that, but you usually don''t need it to exist. it also means that the foreign key lives at the other side. Look for weak and strong entities in database design - Maurício Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) João Pessoa, PB, +55 83 8867-7208 On Fri, Nov 7, 2008 at 2:51 PM, Shandy Nantz <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I am trying to allow a user to answer some questions and then save those > questions but I am unsure if I am getting the layout correct. Here is > what I have so far: > > There is a questions table holding the questions to ask > > There is an answers tables which holds the answers to the questions > asked of a user. > > Then there is of course the Users table which hold a variety of user > info > > I figure my migrations would look something like: > > Questions: has_many :answers > > Answers: belong_to :user, belongs_to :question > > User has_many :answers > > so my table would look like: > > User: id, username, . . . > > Questions: id, question > > Answer: id, user_id, question_id, answer > > Where I am a little confused is if the belongs_to :question should > rather be a has_one :question and would that change the schema at all? > Also, can anyone point me to some good online tutorials involving Model > Relationships, thanks, > > -S > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Shandy Nantz wrote:> I am trying to allow a user to answer some questions and then save those > questions but I am unsure if I am getting the layout correct. Here is > what I have so far: > > There is a questions table holding the questions to ask > > There is an answers tables which holds the answers to the questions > asked of a user. > > Then there is of course the Users table which hold a variety of user > info > > I figure my migrations would look something like: > > Questions: has_many :answers > > Answers: belong_to :user, belongs_to :question > > User has_many :answers > > so my table would look like: > > User: id, username, . . . > > Questions: id, question > > Answer: id, user_id, question_id, answer > > Where I am a little confused is if the belongs_to :question should > rather be a has_one :question and would that change the schema at all? > Also, can anyone point me to some good online tutorials involving Model > Relationships, thanks, > > -SFor what it''s worth, I would probably do something like: User: has_many :questions has_many :answers Question: has_many :answers belongs_to :user Answer: belongs_to :question belongs_to :user Tables: User: id | user_name Question: id | user_id | question Answer: id | user_id | question_id | answer hope this helps -- 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 -~----------~----~----~----~------~----~------~--~---