Rails Model Complex Associations -
i have user model , through table between user's called invite model.
i want users able invite others, invited , have association created.
my migration file looks this:
class createinvites < activerecord::migration def change create_table :invites |t| t.integer :invited_id t.integer :inviter_id t.timestamps end end
in invite model have following:
class invite < activerecord::base belongs_to :invited, :class_name => 'user' belongs_to :inviter, :class_name => 'user' end
i unsure how construct user model appropriate associations. want user belong 1 inviter , have many inviteds. how should update user model appropriately.
class user < activerecord::base has_many :sent_invites, :class_name => "invite", :foreign_key => :inviter_id has_many :inviteds, :through => :sent_invites has_one :invite, :foreign_key => :invited_id has_one :inviter, :through => :invite end
sidebar, idea add indexes foreign keys so:
class createinvites < activerecord::migration def change create_table :invites |t| t.references :invited t.references :inviter t.index :invited_id t.index :inviter_id t.timestamps end end end
Comments
Post a Comment