Hi I have models user and user_status An user always elongs to a particular status at any time Status table has predefined values like "pending","approved" etc My question is what should be the table structure and relation between these models? I am trying like putting status_id in users table And the relation is user belongs_to user_status If so user_status has_one user Is this correct?Please guide me Tom -- 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-/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.
2.7 Choosing Between belongs_to and has_one If you want to set up a 1–1 relationship between two models, you’ll need to add belongs_to to one, and has_one to the other. How do you know which is which? The distinction is in where you place the foreign key (it goes on the table for the class declaring the belongs_to association), but you should give some thought to the actual meaning of the data as well. The has_one relationship says that one of something is yours – that is, that something points back to you. For example, it makes more sense to say that a supplier owns an account than that an account owns a supplier. This suggests that the correct relationships are like this: class Supplier < ActiveRecord::Base has_one :account end class Account < ActiveRecord::Base belongs_to :supplier end The corresponding migration might look like this: class CreateSuppliers < ActiveRecord::Migration def self.up create_table :suppliers do |t| t.string :name t.timestamps end create_table :accounts do |t| t.integer :supplier_id t.string :account_number t.timestamps end end def self.down drop_table :accounts drop_table :suppliers end end I just pasted from The wonderful rails guides:http:// guides.rubyonrails.org/association_basics.html hope it helps, Lake On Jan 5, 1:51 am, Tom Mac <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi > > I have models user and user_status An user always elongs to a > particular status at any time Status table has predefined values like > "pending","approved" etc My question is what should be the table > structure and relation between these models? I am trying like putting > status_id in users table And the relation is > > user belongs_to user_status > > If so user_status has_one user Is this correct?Please guide me > > Tom > -- > 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-/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.
On Jan 5, 6:51 am, Tom Mac <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi > > I have models user and user_status An user always elongs to a > particular status at any time Status table has predefined values like > "pending","approved" etc My question is what should be the table > structure and relation between these models? I am trying like putting > status_id in users table And the relation is > > user belongs_to user_status >If so then your table should be called user_statuses and the column should be user_status_id.> If so user_status has_one user Is this correct?Please guide me >No - since for a given status there are potentially many users in that status (so it''s a has_many) Fred> Tom > -- > 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-/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.
Hi Frederick Thanks for your reply. So what i understood is, tables are users and user_statuses .And users table has a column user_status_id Also the relation is user belongs_to user_status user_status has_many users Is this right? Tom -- 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-/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.