我有一个“兴趣”列表,我系统中的每个用户都可以对其进行评分。管理员可以随时添加/删除兴趣。当用户去编辑他们的帐户时,我想显示系统中所有兴趣的列表,以及一个 1..5 值的下拉列表。我想知道我是如何设置这个的。。
使用 accept_nested_attributes for 似乎不起作用,因为当我使用 field_for 时,它想为每个已保存的兴趣创建表单。我想要的是显示每个兴趣,并在保存时,如果用户之前对兴趣进行了评分,它会更新值,如果之前没有评分,则添加一个新条目。
当前用户:
has_many :user_interests, :dependent => :destroy
has_many :interests, :through => :user_interests, :foreign_key => :user_id
accepts_nested_attributes_for :user_interests
目前是用户兴趣:
belongs_to :user
belongs_to :interest
目前兴趣:
has_many :user_interests, :dependent => :destroy
has_many :users, :through => :user_interests, :foreign_key => :interest_id, :dependent => :destroy