Reputation: 2244
I'm trying to use the Rails Console to create object instances of my models.
It is possible to create and save an instance to the database using the manual methods like so:
1.9.3p125 :003 > subject = Subject.new
=> #<Subject id: nil, name: nil, position: nil, visible: false, created_at: nil, updated_at: nil>
1.9.3p125 :004 > subject.name = "First Name"
=> "First Name"
1.9.3p125 :005 > subject.position = 1
=> 1
1.9.3p125 :006 > subject.visible = 'true'
=> "true"
1.9.3p125 :007 > subject.save
(0.2ms) BEGIN
SQL (0.6ms) INSERT INTO `subjects` (`created_at`, `name`, `position`, `updated_at`, `visible`) VALUES ('2012-06-21 13:28:35', 'First Name', 1, '2012-06-21 13:28:35', 1)
(20.4ms) COMMIT
=> true
1.9.3p125 :008 > subject.id
=> 1
1.9.3p125 :009 > subject.new_record?
=> false
I'm totally new to RoR so I've got no idea where to begin trouble shooting this error:
1.9.3p125 :010 > subject = Subject.create(:name => "Second name", :position => 2)
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: name, position from /Users/Nick/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
from /Users/Nick/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
from /Users/Nick/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
from /Users/Nick/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
from /Users/Nick/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.3/lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
from /Users/Nick/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.3/lib/active_record/base.rb:498:in `initialize'
from /Users/Nick/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.3/lib/active_record/persistence.rb:44:in `new'
from /Users/Nick/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.3/lib/active_record/persistence.rb:44:in `create'
from (irb):10
from /Users/Nick/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /Users/Nick/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/Nick/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Here is my Subject model:
class Subject < ActiveRecord::Base
attr_accessible :title, :body, :name, :position
end
Thanks!
Things I've tried so far:
Upvotes: 0
Views: 359
Reputation: 21
i ran into the same problem the solution was to do it this way
subject=Subject.create(name: 'Second name', position: 2)
it works fine for me
Upvotes: 0
Reputation: 8954
You need to add
attr_accessible :name, :position
To your Subject
model so you can create this in one step. Its a thing of security. You can read about this here:
hf,..
Upvotes: 1