Ruby-on-rails Rails 一对一关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15098586/
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
Rails one-to-one relationship
提问by cgf
I have the following:
我有以下几点:
class User < ActiveRecord::Base
has_one :car, :class_name => 'Car', :foreign_key => 'user_id'
class Car < ActiveRecord::Base
belongs_to :worker, :class_name => 'User', :foreign_key => 'user_id'
It is basically a one-to-one relationship between a user and a car.
它基本上是用户和汽车之间的一对一关系。
What I want is for the User to be able to have one and only one car. That implies the fact that if he creates a car assigned to him, he won't be able to create the second.
我想要的是让用户能够拥有一辆而且只有一辆汽车。这意味着如果他创建分配给他的汽车,他将无法创建第二个。
How could this be done?
这怎么可能?
采纳答案by AdamT
why don't you just test before the user tries to add a car?
为什么不在用户尝试添加汽车之前进行测试?
if worker.car
raise "sorry buddy, no car for you"
else
car = Car.create(user_id: worker.id)
end
回答by Robert
There are certainly a couple different ways of accomplishing this. I would suggest creating a composite key index on that table to ensure that the user_id is unique in the table. This will ensure that it will only be present once. In a migration, you could write something like this.
当然有几种不同的方法可以实现这一点。我建议在该表上创建一个复合键索引,以确保 user_id 在表中是唯一的。这将确保它只会出现一次。在迁移中,您可以编写类似这样的内容。
add_index(:cars, :worker_id, :unique => true)
The first argument is the table name (don't forget this is generally the pluralized version of the class name). The field name comes second. The unique true is what will prevent you from inserting an extra row.
第一个参数是表名(不要忘记这通常是类名的复数形式)。字段名称排在第二位。唯一的 true 将阻止您插入额外的行。
Note: This is a database level constraint. If you hit this because validations didn't catch it, it will throw an error.
注意:这是一个数据库级别的约束。如果你因为验证没有发现它而点击它,它会抛出一个错误。
In addition to this solution, you will want to add a validation to the Car model itself.
除了此解决方案之外,您还需要向 Car 模型本身添加验证。
validates_uniqueness_of :worker_id, message: "can not have more than one car"
You'll see this error come through with something like "Worker ID can not have more than one car". You will most likely want to customize the "Worker ID" section of this. Refer to this postfor instructions on how to do that.
你会看到这个错误是通过类似“工人 ID 不能有超过一辆汽车”这样的东西来实现的。您很可能希望自定义此内容的“工作人员 ID”部分。有关如何执行此操作的说明,请参阅此帖子。
You certainly don't have to do the db constraint, but in case anyone else inserts into the DB, it's a good idea. Otherwise, you'll have "invalid" data as far as Rails is concerned.
您当然不必执行 db 约束,但万一其他人插入到数据库中,这是一个好主意。否则,就 Rails 而言,您将拥有“无效”数据。
回答by Richard Brown
Change the definition of the relationship slightly:
稍微改变关系的定义:
class User < ActiveRecord::Base
has_one :car
class Car < ActiveRecord::Base
belongs_to :worker, :class_name => 'User', :foreign_key => 'user_id'
And you'll establish what you're looking for. See: http://guides.rubyonrails.org/association_basics.html#the-has-one-association
您将确定您正在寻找的内容。请参阅:http: //guides.rubyonrails.org/association_basics.html#the-has-one-association

