ruby on rails - ActiveRecord::UnknownAttributeError in UserController#create -


i have started learning rails , purpose begin developing shipping app nested attributes. have model user , box , boxkind table habtm between box , kind.

user model

class user < activerecord::base has_many :boxes accepts_nested_attributes_for :boxes end 

box model

class box < activerecord::base     belongs_to :user, :foreign_key => "user_id"     accepts_nested_attributes_for :user  has_and_belongs_to_many :kinds, join_table: :boxes_kinds     accepts_nested_attributes_for :kinds end 

kind model

class kind < activerecord::base has_and_belongs_to_many :boxes, join_table: :boxes_kinds end 

when try add new record database getting unknown attribute: box_id error. bit confusing me hence have added custom primary key box model called ref_no.

where wrong ?

update @nitinverma requested in addition console log :

started post "/user" 127.0.0.1 @ 2014-08-18 19:27:11 +1000 processing usercontroller#create html   parameters: {"utf8"=>"✓", "authenticity_token"=>"xt5ovi9hfu98rhq0fgb5ndui1lrg0bned8+03hurr1y=", "user"=>{"name"=>"mark", "email"=>"mark@abc.com", "address"=>"some address ", "postcode"=>"1928", "tel_no"=>"0394884994", "state"=>"vic", "boxes_attributes"=>{"0"=>{"ref_no"=>"1005", "quantity"=>"2", "kinds_attributes"=>{"1408354027248"=>{"big"=>"2", "small"=>"", "odd"=>"", "trunck"=>"", "_destroy"=>"false"}, "0"=>{"big"=>"", "small"=>"",  "_destroy"=>"1"}}, "collected_at(1i)"=>"2012", "collected_at(2i)"=>"8", "collected_at(3i)"=>"18", "collected_at(4i)"=>"08", "collected_at(5i)"=>"59", "destination_country"=>"uk", "destination_country_address"=>"regency  street 18", "shipped"=>"1", "shipped_at(1i)"=>"2014", "shipped_at(2i)"=>"8", "shipped_at(3i)"=>"18", "shipped_at(4i)"=>"08", "shipped_at(5i)"=>"59", "reached"=>"0", "reached_at(1i)"=>"2014", "reached_at(2i)"=>"8", "reached_at(3i)"=>"18", "reached_at(4i)"=>"08", "reached_at(5i)"=>"59"}}}, "commit"=>"submit"}  unpermitted parameters: _destroy unpermitted parameters: _destroy  completed 500 internal server error in 32ms  activerecord::unknownattributeerror (unknown attribute: box_id):   app/controllers/user_controller.rb:21:in `create' 

habtm

i think problem habtm table:

create_table "boxes_kinds", id: false, force: true |t|     t.integer "ref_no",  null: false     t.integer "kind_id", null: false end 

rails has_and_belongs_to_many tables meant contain foreign_key each of associated tables:

enter image description here

the problem have since your table has box_id ref_no, rails unable determine column save value to; hence invoking exception you're seeing.

i recommend using association_foreign_key or foreign_key arguments has_and_belongs_to_many association:

#app/models/box.rb class box < activerecord::base    has_and_belongs_to_many :kinds, foreign_key: "ref_no" end  #app/models/kind.rb class kind < activerecord::base    has_and_belongs_to_many :boxes, association_foreign_key: "ref_no" end 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

python - Django-cities exits with "killed" -

python - How to get a widget position inside it's layout in Kivy? -