使用cocoon通过关联形成has_many的Rails表单

时间:2014-11-13 12:35:23

标签: ruby-on-rails ruby drop-down-menu simple-form cocoon-gem

我在桥牌表skill_users中使用了用户和技能之间的多次关系。我用过茧宝石。我在每个add_association上使用下拉框获取技能值。在添加新技能时,下拉列表显示所有值,但我只想要未选择的值。有没有办法拒绝已经从新技能的dropbox中选择的值

class SkillsUser < ActiveRecord::Base
  belongs_to :skill
 belongs_to :user
end

class Skill < ActiveRecord::Base
 has_many :skills_users
 has_many :users, through: :skills_users
 belongs_to :skill_type
end

class User < ActiveRecord::Base
 has_many :skills_users
 has_many :skills, through: :skills_users
end

这是我的技能下拉框代码

 = f.input :skill_id, as: :select, collection: Skill.where(skill_type: skill_type.id), label_method: :name, value_method: :id,  label: false,  placeholder: 'Add Skills'

1 个答案:

答案 0 :(得分:0)

在你的控制器中添加:

@available_skills = Skill.where(skill_type: skill_type).where("`id` not in ?", @user.skills.pluck(:id))

然后在你看来:

 = f.input :skill_id, as: :select, collection: @available_skills, label_method: :name, value_method: :id,  label: false,  placeholder: 'Add Skills'