ActiveRecord :: UnknownAttributeError in UserController # create

I started by learning about rails, and for this purpose I start by developing a nested attribute delivery application. Basically I have a table User

, Box

and BoxKind

with a HABTM table between Box

and 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 I try to add a new record to the database, I get an error unknown attribute: box_id

. This confuses me a little, so I added a custom primary key to the model Box

called ref_no

.

Where am I going wrong?

UPDATE . Since @NitinVerma is requested additionally, this is the console log:

Started POST "/user" for 127.0.0.1 at 2014-08-18 19:27:11 +1000
Processing by UserController#create as 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'

      

+3


source to share


2 answers


HABTM

I think the problem is with your table habtm

:

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

      

Rails tables has_and_belongs_to_many

are meant to be placed foreign_key

for each of the related tables:



enter image description here

The problem is that because your table has a box_id

how ref_no

, Rails cannot define a column to store the value; hence, referring to the exception you are seeing.

I would recommend using arguments association_foreign_key

or foreign_key

for your association has_and_belongs_to_many

:

#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

      

+3


source


you need to modify the box_kinds table. remove ref_no and add box_id. Since the join table stores the id column of the related tables eg #{model_name}_id

. Also you need to remove the id column from the custom permission options or specify the parent permission options. id is only required for child models. Also, if you want to destroy any associated child when editing the parent, you can add _destory to resolve the parameters.



def user_box_params

    params.require(:user).permit(:name, :email, :address, :postcode, :tel_no, :state, 

     boxes_attributes: [:id, :ref_no, :quantity, :collected_at, :destination_country, :destination_country_address, :shipped, :shipped_at, :reached, :reached_at, _destroy,

     kinds_attributes: [:id, :big, :small, :odd, :trunck, _destroy]]) 
end

      

0


source







All Articles