My create method does not take any parameters, rather it uses a sister object to assign the values. The fields are almost the same, but not quite.
I would also like to make the create method simpler (instead of doing a .new and then .save).
I would like the code below to be my starting point
controller
def create
design_setting = DesignSetting.first
primer3_parameter = Primer3Parameter.new
primer3_parameter.update_attributes(design_setting.attributes)
primer3_parameter.save
render json: primer3_parameter, root: false
end
def safe_params
params.permit(:generated, :status, :p3_file_id, :p3_file_flag,
...etc...
model
class Primer3Parameter < ActiveRecord::Base
has_many :batch_details
end
error
Started POST "/api/primer3_parameters" for 10.0.2.2 at 2014-06-11 23:15:38 +0200
Processing by Api::Primer3ParametersController#create as JSON
Parameters: {"base"=>{}}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
DesignSetting Load (1.1ms) SELECT "design_settings".* FROM "design_settings" ORDER BY "design_settings"."id" ASC LIMIT 1
(0.2ms) BEGIN
(0.3ms) ROLLBACK
Completed 500 Internal Server Error in 350ms
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes for Primer3Parameter: type, id):
app/controllers/api/primer3_parameters_controller.rb:12:in `create'
EDIT
After refactoring as suggested below the same error appears:
Started POST "/api/primer3_parameters" for 10.0.2.2 at 2014-06-11 23:27:25 +0200
Processing by Api::Primer3ParametersController#create as JSON
Parameters: {"base"=>{}}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
DesignSetting Load (1.2ms) SELECT "design_settings".* FROM "design_settings" ORDER BY "design_settings"."id" ASC LIMIT 1
Completed 500 Internal Server Error in 172ms
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes for Primer3Parameter: type, id):
app/controllers/api/primer3_parameters_controller.rb:12:in `create'
EDIT 2
Version 2 of the code
def create
design_setting = DesignSetting.first
primer_attributes = design_setting.attributes.except(:id, :created_at, :updated_at, :type)
primer_attributes = design_setting.dup
Rails.logger.debug("attributes #{primer_attributes}")
primer3_parameter = Primer3Parameter.create(primer_attributes)
render json: primer3_parameter, root: false
end
Started POST "/api/primer3_parameters" for 10.0.2.2 at 2014-06-11 23:33:21 +0200
Processing by Api::Primer3ParametersController#create as JSON
Parameters: {"base"=>{}}
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
DesignSetting Load (1.5ms) SELECT "design_settings".* FROM "design_settings" ORDER BY "design_settings"."id" ASC LIMIT 1
attributes #<DesignSetting:0x0000000cca1988>
Completed 500 Internal Server Error in 163ms
NoMethodError (undefined method `stringify_keys' for #<DesignSetting:0x0000000cca1988>):
app/controllers/api/primer3_parameters_controller.rb:14:in `create'
EDIT 3
def create
design_setting = DesignSetting.first
primer_attributes = design_setting.attributes.except(:id, :type, :created_at, :updated_at)
primer3_parameter = Primer3Parameter.create(primer_attributes)
render json: primer3_parameter, root: false
end
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes for Primer3Parameter: type, id):
app/controllers/api/primer3_parameters_controller.rb:12:in `create'
EDIT4
This table has a lot of fields, so not all are pasted below.
I did notice something - the id is the last column, not the first. But it should not matter?
DesignSetting Load (1.3ms) SELECT "design_settings".* FROM "design_settings" ORDER BY "design_settings"."id" ASC LIMIT 1
parms: {"type"=>nil, "user_id"=>nil, "generated"=>nil, "status"=>nil, "p3_file_type"=>nil, "p3_file_id"=>"empty", "p3_file_flag"=>false, "p3_comment"=>"empty", "sequence_excluded_region"=>"empty", "sequence_force_left_end"=>nil