I put collection_check_boxes for HABTM case, and i getting this error:
Started POST "/cars" for 127.0.0.1 at 2014-09-18 13:11:23 -0300
Processing by CarsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"b6MdXqwIDw2GvgEGvY1dGEr3v08+h9IkC5UPFyjX6MA=", "car"=>{"brand"=>"4", "model"=>"1117", "version"=>"8417", "license_plate"=>"hjg1234", "year"=>"133", "year_fabrication"=>"134", "steelplated"=>"false", "price"=>"12.34", "car_body_type_id"=>"2", "car_gearshift_id"=>"1", "condition"=>"new", "ports_number"=>"2", "color"=>"31", "car_fuel_type_id"=>"3", "renavam"=>"123421352", "optional"=>"", "tag_ids"=>["2", "4", "6", ""]}, "commit"=>"Confirmar"}
User Load (1.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Tag Load (0.5ms) SELECT "tags".* FROM "tags" WHERE "tags"."id" IN (2, 4, 6)
Completed 500 Internal Server Error in 18ms
ActiveRecord::UnknownPrimaryKey (Unknown primary key for table cars_tags in model CarsTag.):
app/controllers/cars_controller.rb:62:in `create'
Rendered /home/yanksan/.rvm/gems/ruby-2.1.2@carbon/gems/actionpack-4.0.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.1ms)
Rendered /home/yanksan/.rvm/gems/ruby-2.1.2@carbon/gems/actionpack-4.0.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.6ms)
Rendered /home/yanksan/.rvm/gems/ruby-2.1.2@carbon/gems/actionpack-4.0.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.4ms)
Rendered /home/yanksan/.rvm/gems/ruby-2.1.2@carbon/gems/actionpack-4.0.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (15.6ms)
Added code:
View:
<%= f.collection_check_boxes :tag_ids, Tag.all, :id, :name %>
Controller (added tag_ids: []):
def car_params
params.require(:car).permit(:carname, :brand, :model, :version, :year, :year_fabrication, :condition, :mileage,
:price, :color, :optional, :license_plate, :steelplated, :car_body_type_id,
:car_gearshift_id, :ports_number, :car_fuel_type_id, :renavam, tag_ids: [])
end
...
def create
@user = User.find(current_user)
@car = @user.cars.new(car_params)
respond_to do |format|
if @car.save
CarsUser.where('user_id = ? AND car_id = ?', current_user, @car).update_all(date: Time.now)
car = @user.cars.find(@car)
format.html { redirect_to car, notice: 'Car was successfully created.' }
format.json { render action: 'show', status: :created, location: @car }
else
format.html { render action: 'new' }
format.json { render json: @car.errors, status: :unprocessable_entity }
end
end
end
Car model:
class Car < ActiveRecord::Base
has_many :cars_tags
has_many :tags, through: :cars_tags
end
Tag model:
class Tag < ActiveRecord::Base
has_many :cars_tags
has_many :cars, through: :cars_tags
end
CarsTag model:
class CarsTag < ActiveRecord::Base
belongs_to :car
belongs_to :tag
end
I have anothers associations like this in my code, but only place i get error is here when i use collection_check_boxes.
Rails version: 4.0.8
Ruby version: 2.1.2p95
Can anyone help me?