ruby on rails - Designing Two Models to Have A Complex Association -
i have 2 tables, users , groups. user owns grouping , can apart of multiple groups. grouping belongs 1 user , can have many users.
thus user model have
has_and_belongs_to_many :groups has_many :groups while group model have
has_and_belongs_to_many :users belongs_to :user i have bring together table in migrations..
def alter create_table :groups_users, :id => false |t| t.integer :group_id t.integer :user_id end end my question create sense? sense i'm doing wrong having has_many , belongs_to on top of has_and_belongs_to_many.
the way approach this, , own personal methodology, 3 tables/models so:
group_user.rb
class groupuser < activerecord::base attr_accessible :user_id, :group_id belongs_to :group belongs_to :user end group.rb
class grouping < activerecord::base attr_accessible :owner_id validates_presence_of :owner_id has_many :group_users has_many :users, through: :group_users end user.rb
class user < activerecord::base attr_accessible :some_attributes has_many :group_users has_many :groups, through: :group_users end then, whenever create group object, user created have id placed in owner_id attribute of group , groupuser table.
ruby-on-rails ruby-on-rails-3 join associations rails-activerecord
No comments:
Post a Comment