У меня есть модель пользователя, которая has_many :topics, :through => :topification
.В моей Topification
модели у меня есть topic_id
и user_id
.
Но я не хочу, чтобы user_id
был назначен по массе (чтобы избежать ситуации, когда один пользователь задает тему для другого пользователя), поэтому я исключаю это из моего attr_accessible :topic_id
заявления.
Однако, если я создам новую тему для пользователя, я получу WARNING: Can't mass-assign protected attributes: user_id
Каким будет способ Rails справиться с этой ситуацией?Это делается для того, чтобы избежать массового назначения в модели соединения отношений «многие ко многим».
Обновление: так выглядят мои Модели и форма:
class User < ActiveRecord::Base
has_many :topifications, :dependent => :destroy
has_many :topics, :through => :topifications, :dependent => :destroy
attr_accessible :topics_attributes, :topic_ids
validates_associated :topics
end
class Topic < ActiveRecord::Base
attr_accessible :title, :description
has_many :topifications, :dependent => :destroy
has_many :users, :through => :topifications
end
class Topification < ActiveRecord::Base
belongs_to :topic
belongs_to :user
attr_accessible :topic_id
end
В моей форме
- Topic.all.each do |topic|
= check_box_tag "user[topic_ids][]", topic.id,
@user.topic_ids.include?(topic.id), id: dom_id(topic)
= label_tag dom_id(topic), topic.title