开发者

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

(I am using Rails 2.2.2, but should be very simil开发者_运维百科ar to 2.3.5 or 3.0)

The following line works:

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

But I generated a migration and added identifier to the users table, and restarted the Rails console, and used

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

This user is created with and the email field is set to bar (as seen in mysql) but identifier is not 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


Try adding attr_accessible to User model:

def User
  attr_accessible :identifier
end

If you do not want to add attr_accessible for identifier (because say, a user should not be allowed to set their own identifier), then you need to first save the user and then set the identifier separately:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜