如何避免在使用Ruby on Rails 3的种子过程中的验证,回调和'attr_accessible'效果? [英] How to avoid the validation, callbacks and 'attr_accessible' effects during the seeding process using Ruby on Rails 3?
问题描述
在'RAILS_ROOT / models / user.rb'中我有:。我使用Ruby on Rails 3,我试图在我的应用程序数据库中种子数据。
class User< ActiveRecord :: Base
attr_accessible #none
validates:name,
:presence => true
validates:surname,
:presence => true
validates:email,
:presence => true
end
在RAILS_ROOT / db / seeds.rb p>
#Test 1
User.find_or_create_by_email(
:name =>Test1 name,
:surname =>Test1 surname,
:email =>test1@test1.test1
)
#Test2
User.find_or_create_by_email
:name =>,
:surname =>,
:email =>test2@test2.test2
)
因此,在终端中运行
rake db:seed
当然,数据库不会填充数据,因为'attr_accessible'到nil(Case Test1)和验证未通过(Case Test2)。
我想在播种期间跳过验证和attr-访问效果 有可能吗?
PS:我不想使用'RAILS_ROOT / db / migrate / 201 .... rb '代码如下:
执行INSERT INTO用户(姓名,电子邮件)VALUES(Test1 name,Test1 surname,test1@test1.test1)
UPDATE
我还需要略过所有回拨。 是可能吗?如果是,如何检查 org / classes / ActiveRecord / Base.html#method-i-attributes%3Drel =noreferrer> ActiveRecord的文档 attributes =
方法具有启用此功能的参数:
attributes =(new_attributes,guard_protected_attributes = true)
使用它像这样:
#创建一个新用户
@user = User.new
#用户的属性
@attrib = {
:name => Test1 name,
:surname => Test1 surname,
:email => test1@test1.test1
}
#使用'send'调用对象的attributes =方法
@ user.send:attributes =,@attrib,false
#保存对象
@ user.save
I am using Ruby on Rails 3 and I am trying to seed data in my application database.
In 'RAILS_ROOT/models/user.rb' I have:
class User < ActiveRecord::Base
attr_accessible #none
validates :name,
:presence => true
validates :surname,
:presence => true
validates :email,
:presence => true
end
In 'RAILS_ROOT/db/seeds.rb' I have:
# Test 1
User.find_or_create_by_email (
:name => "Test1 name",
:surname => "Test1 surname",
:email => "test1@test1.test1"
)
# Test2
User.find_or_create_by_email (
:name => "",
:surname => "",
:email => "test2@test2.test2"
)
So, running in the Terminal
rake db:seed
of course the database will NOT populate with datas because 'attr_accessible' to nil (Case Test1) and validation not passed (Case Test2).
I would like to skip the validation and "attr-accessible effects" during the seeding process. Is it possible? If so, how to do that?
P.S.: I don't want to use in 'RAILS_ROOT/db/migrate/201....rb' code like this:
execute "INSERT INTO users ( name, surname, email ) VALUES ( "Test1 name", "Test1 surname", "test1@test1.test1")"
UPDATE
I need also to skip all callbacks. Is it possible? If so, how?
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
这篇关于如何避免在使用Ruby on Rails 3的种子过程中的验证,回调和'attr_accessible'效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!