Ruby on rails:创建具有belongs_to 关联的模型条目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16286944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby on rails: Creating a model entry with a belongs_to association
提问by user2158382
I am trying to add a new entry in my database for a model that has a belongs_to relationship. I have 2 models, Jobs and Clients.
我正在尝试在我的数据库中为具有belongs_to 关系的模型添加一个新条目。我有 2 个模型,Jobs 和 Clients。
It was easy enough to find tutorial on how to set up the association between these two (using has_many and belongs_to), but I can't seem to find any examples where the association is actually used.
很容易找到关于如何在这两者之间建立关联的教程(使用 has_many 和belongs_to),但我似乎找不到任何实际使用关联的示例。
In my code, I am trying to create a new job for the first client. The jobs model has an attribute for client_id, and I know I can probably just manually fill the attribute, but there has to be some ruby convention to easily accomplish this.
在我的代码中,我试图为第一个客户创建一个新工作。作业模型有一个 client_id 属性,我知道我可能只能手动填充该属性,但必须有一些 ruby 约定才能轻松完成此操作。
Job.create(:client_id => 1, :subject => "Test", :description => "This is a test")
I can easily put that in my code, but I feel like ruby has a better way to do this. Here is the way my models are setup
我可以很容易地把它放在我的代码中,但我觉得 ruby 有更好的方法来做到这一点。这是我的模型设置方式
class Job < ActiveRecord::Base
attr_accessible :actual_time, :assigned_at, :client_id, :completed_at, :estimated_time, :location, :responded_at, :runner_id, :status, :subject, :description
belongs_to :client
end
class Client < User
has_many :jobs
end
class User < ActiveRecord::Base
attr_accessible :name, :cell, :email, :pref
end
回答by alf
Just call createon the jobscollection of the client:
只需调用客户端create的jobs集合:
c = Client.find(1)
c.jobs.create(:subject => "Test", :description => "This is a test")
回答by MrYoshiji
You can pass the object as argument to create the job:
您可以将对象作为参数传递以创建作业:
client = Client.create
job = Job.create(client_id: client.id, subject: 'Test', description: 'blabla')
The createmethod will raise an error if the object is not valid to save (if you set validations like mandatory name, etc).
create如果对象无法保存,则该方法将引发错误(如果您设置了强制名称等验证)。
回答by Rory O'Kane
Pass the object itself as an argument, instead of passing its ID. That is, instead of passing :client_id => 1or :client_id => client.id, pass :client => client.
将对象本身作为参数传递,而不是传递其 ID。也就是说,不是通过:client_id => 1or :client_id => client.id,而是pass :client => client。
client = Client.find(1)
Job.create(:client => client, :subject => "Test", :description => "This is a test")
回答by Meysam Salehi
You can use create_jobin this way:
你可以这样使用create_job:
client = Client.create
job = client.create_job!(subject: 'Test', description: 'blabla')
When you declare a belongs_toassociation, the declaring class automatically gains five methods related to the association:
当你声明一个belongs_to关联时,声明类会自动获得与关联相关的五个方法:
association
association=(associate)
build_association(attributes = {})
create_association(attributes = {})
create_association!(attributes = {})
In all of these methods, association is replaced with the symbol passed as the first argument to belongs_to.
在所有这些方法中,关联被替换为作为第一个参数传递给 的符号belongs_to。
more: http://guides.rubyonrails.org/association_basics.html#belongs-to-association-reference
更多:http: //guides.rubyonrails.org/association_basics.html#belongs-to-association-reference
回答by MentholBonbon
For creating new instances you could use factories. For this you could simply use FactoryGirl https://github.com/thoughtbot/factory_girl
要创建新实例,您可以使用工厂。为此,您可以简单地使用 FactoryGirl https://github.com/thoughtbot/factory_girl
So after you have defined your factory soewhat like this:
因此,在您像这样定义工厂之后:
FactoryGirl.define do factory :job do client FactoryGirl.create(:client) subject 'Test' description 'This is a Test'
FactoryGirl.define do factory :job do client FactoryGirl.create(:client) subject 'Test' description 'This is a Test'
You could then call FactoryGirl.create(:job) to generate a new job like that. You could also call FactoryGirl.build(:job, client: aClientYouInstantiatedBefore, subject: 'AnotherTest') and also overwrite any other attributes
然后,您可以调用 FactoryGirl.create(:job) 来生成这样的新作业。您还可以调用 FactoryGirl.build(:job, client: aClientYouInstantiatedBefore, subject: 'AnotherTest') 并覆盖任何其他属性
Factores are good if you want to create many objects, that are similar in a certain way.
如果你想创建许多在某种方式上相似的对象,因子是很好的。

