ruby - Rails forms for has_many through association using cocoon -
i have used has-many-through relation between user , skills bridge table skills_users. have used cocoon gem. getting values skills using dropdown box on each add_association. in adding new skill dropdown shows values want values have not been selected. there way reject values have been selected dropbox of new skills
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
here drop-down box code skills
= 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'
in controller add:
@available_skills = skill.where(skill_type: skill_type).where("`id` not in ?", @user.skills.pluck(:id))
then in view:
= f.input :skill_id, as: :select, collection: @available_skills, label_method: :name, value_method: :id, label: false, placeholder: 'add skills'
Comments
Post a Comment