How to avoid the validation, callbacks and 'attr_accessible' effects during the seeding process using Ruby on Rails 3?

佐手、 提交于 2019-11-30 16:48:07

If you check ActiveRecord's documentation you'll see the attributes= method has a parameter to enable this:

attributes=(new_attributes, guard_protected_attributes = true)

Use it like this:

# Create a new user
@user = User.new

# Attributes for the user
@attrib = {
  :name       => "Test1 name",
  :surname    => "Test1 surname",
  :email      => "test1@test1.test1"
}

# Use 'send' to call the attributes= method on the object
@user.send :attributes=, @attrib, false

# Save the object
@user.save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!