ruby - How to setup relation so I can add multiple associations to self? -
ruby - How to setup relation so I can add multiple associations to self? -
i have 2 classes
class project < activerecord::base has_many :related_projects has_many :projects, through: :related_projects end class relatedproject < activerecord::base belongs_to :project belongs_to :related_project, class_name: 'project', foreign_key: 'related_project_id' end
relatedproject association table stores projects related each other. project has many-to-many relation itself.
what can't figure out how set can like
project.project_ids = [2,3] update project have 2 related projects. should automatically add together association on save. works fine if not doing many many relationship same model.
what missing?
if understand correctly, think need utilize has_and_belongs_to_many
create many many association table. replace relatedproject model.
look @ docs relation.
ruby ruby-on-rails-4 model-associations
Comments
Post a Comment