I have models
class Riskfactor < ActiveRecord::Base
has_many :bodies_riskfactors
has_many :bodies, through: :bodies_riskfactors
end
class Body < ActiveRecord::Base
has_many :bodies_riskfactors
has_many :riskfactors, through: :bodies_riskfactors
end
class DisordersArticle < ActiveRecord::Base
belongs_to :disorder
belongs_to :article
end
in view
= form_for [:admin, @riskfactor], html: {role: "form"} do |f|
= f.select :body_ids, Body.all.collect {|x| [x.name, x.id]}, {}, :multiple => true
and I have error on update
What could be wrong?
Processing by Admin::RiskfactorsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"CVkdBHLzLAeQ8FRFBVyonUuyn1FZAYG+X9fX/mmj+Bo=", "riskfactor"=>{"body_ids"=>["", "1"], "position"=>"1", "slug"=>"risk-factor-2", "name_ru"=>"High Blood Pressure", "published_ru"=>"no", "content_ru"=>"", "meta_description_ru"=>"", "meta_keywords_ru"=>"", "name_en"=>"High Blood Pressure", "published_en"=>"no", "content_en"=>"
High Blood Pressure High Blood Pressure High Blood Pressure High Blood Pressure High Blood Pressure
users
.* FROM users
WHERE users
.id
= 1 ORDER BY users
.id
ASC LIMIT 1
Riskfactor Load (0.3ms) SELECT riskfactors
.* FROM riskfactors
WHERE riskfactors
.id
= 4 LIMIT 1
(0.1ms) BEGIN
Body Load (0.3ms) SELECT bodies
.* FROM bodies
WHERE bodies
.id
= 1 ORDER BY position LIMIT 1
Body Load (2.6ms) SELECT bodies
.* FROM bodies
INNER JOIN bodies_riskfactors
ON bodies
.id
= bodies_riskfactors
.body_id
WHERE bodies_riskfactors
.riskfactor_id
= 4 ORDER BY position
Riskfactor::Translation Load (0.3ms) SELECT riskfactor_translations
.* FROM riskfactor_translations
WHERE riskfactor_translations
.riskfactor_id
= 4
Riskfactor Exists (0.6ms) SELECT 1 AS one FROM riskfactors
WHERE (riskfactors
.slug
= 'risk-factor-2' AND riskfactors
.id
!= 4) LIMIT 1
(0.2ms) ROLLBACK
Completed 500 Internal Server Error in 15ms
NoMethodError (undefined method body' for #<Riskfactor:0x007f9a0351c350>):
app/controllers/admin/riskfactors_controller.rb:45:in
block in update'
app/controllers/admin/riskfactors_controller.rb:44:in `update'
RiskfactorsController
class Admin::RiskfactorsController < Admin::BaseController
before_action :set_riskfactor, only: [:show, :edit, :update, :destroy]
def update
respond_to do |format|
if @riskfactor.update(riskfactor_params)
format.html { redirect_to [:edit, :admin, @riskfactor], notice: 'Riskfactor was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @riskfactor.errors, status: :unprocessable_entity }
end
end
end
private
def set_riskfactor
@riskfactor = Riskfactor.find(params[:id])
end
def riskfactor_params
params.require(:riskfactor).permit!
end
end
has_many ... through
relation, so the foreign keys are on thethrough
table (bodies_riskfactors
). – DaveMongoose