I have three models, a user, a profile and a role, where I'm trying to attach multiple roles to a user through profile.
# user.rb
class User < ActiveRecord::Base
has_one :profile, dependent: :destroy
has_many :roles, through: :profile
has_many :interests, through: :profile
has_many :industries, through: :profile
end
# profile.rb
class Profile < ActiveRecord::Base
has_one :user, dependent: :destroy
has_many :roles
has_many :interests
has_many :industries
end
# role.rb
class Role < ActiveRecord::Base
has_many :profiles
has_many :users, :through => :profiles
has_many :users
end
I'm trying to use the following in edit.html.erb (simple_form_for)
<%= f.association :roles,
as: :check_boxes,
label_method: :name,
value_method: :id,
label: 'Roles' %>
With the following params
params.require(:user).permit([:email, :first_name, :last_name, :admin, :password, role_ids: [] ])
My edit/update method:
# GET /users/1/edit
def edit
@user.build_profile if @user.profile.nil?
end
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
Gives me the following error message
Cannot modify association 'User#roles' because the source reflection class 'Role' is associated to 'Profile' via :has_many.
I've been looking at the problem for hours, but I can't seem to find the mistake.
Aucun commentaire:
Enregistrer un commentaire