Why Ruby on Rails' User.create! (: Email => 'ha',: identifier => 'foo') does not work?

(I am using Rails 2.2.2, but should be very similar to 2.3.5 or 3.0)

The following line works:

User.create!(:email => 'ha')

But I generated the migration and added the identifierusers to the table, restarted the Rails console and used

User.create!(:email => 'bar', :identifier => 'foo')    

This user is created and the email field is set to bar(as seen from mysql), but identifiernot set to foo... is there a reason why?


db/schema.rb:

create_table "users", :force => true do |t|
  t.string   "login"
  t.string   "email"
  t.string   "crypted_password",                  :limit => 40
  t.string   "salt",                              :limit => 40
  t.datetime "created_at"
    [...]
  t.string   "short_bio"
  t.string   "identifier"
end
+3
source share
1 answer

Try adding the model attr_accessibleto User:

def User
  attr_accessible :identifier
end

attr_accessible identifier ( , , ), , :

User.create!(:email => "a@a.com")

u = User.find_by_email("a@a.com")
u.identifier = "foo"
u.save!
+2

Source: https://habr.com/ru/post/1792389/


All Articles