Rails:拥有并属于许多(HABTM)——创建关联而不创建其他记录
谷歌了一整天,也没找到答案。 :\
我在用户和 Core_Values 之间有 HABTM 关系。
class CoreValue < ActiveRecord::Base
has_and_belongs_to_many :users
class User < ActiveRecord::Base
has_and_belongs_to_many :core_values
在我的控制器中,我需要做两件事:
- 如果 CoreValue 不存在,则创建一个新的 CoreValue 并将其与给定的用户 ID 关联,并且
- 假设我知道特定的 CoreValue 确实已存在,则创建关联而不创建任何新的CoreValues 或用户
对于#1,我已经让它工作了:
User.find(current_user.id).core_values.create({:value => v, :created_by => current_user.id})
这将使用 :value 和 :created_by 创建一个新的 CoreValue 并创建关联。
对于#2,我尝试了一些方法,但似乎无法仅创建关联。
感谢您的帮助!
Spent all day on Google, but can't find an answer. :\
I have a HABTM relationship between Users and Core_Values.
class CoreValue < ActiveRecord::Base
has_and_belongs_to_many :users
class User < ActiveRecord::Base
has_and_belongs_to_many :core_values
In my controller, I need to do two separate things:
- If a CoreValue does not exist, create a new one and associate it with a given user id, and
- Assuming I know a particular CoreValue does exist already, create the association without creating any new CoreValues or Users
For # 1, I've got this to work:
User.find(current_user.id).core_values.create({:value => v, :created_by => current_user.id})
This creates a new CoreValue with :value and :created_by and creates the association.
For # 2, I've tried a few things, but can't quite seem to create the association ONLY.
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用非常有用的
find_or_create
方法通过两步过程来完成此操作。find_or_create
将首先尝试查找记录,如果不存在,则创建它。像这样的事情应该可以解决问题:一些注意事项:
v
。如果它不存在但已创建,则会将created_by
设置为current_user.id
。User.find(current_user.id)
,因为这会返回与current_user
相同的对象。current_user.core_values
是一个数组,您可以使用<<
轻松向其中添加另一个值。为简洁起见,以下内容与上面的代码示例相同:
You can do this in a two-step procedure, using the very useful
find_or_create
method.find_or_create
will first attempt to find a record, and if it doesn't exist, create it. Something like this should do the trick:Some notes:
v
. If it doesn't exist and is created, it will set thecreated_by
tocurrent_user.id
.User.find(current_user.id)
, as that would return the same object ascurrent_user
.current_user.core_values
is an array, and you can easily add another value to it by using<<
.For brevity, the following would be the same as the code example above:
在您的用户模型中添加一个方法:
assign_core_value
方法满足要求 1,2 并返回分配给用户(具有给定名称)的核心值。现在您可以执行以下操作:
注释1
方法逻辑如下:
1) 检查CoreValue 是否已与用户关联。如果是,则不采取任何操作,返回核心值。
2) 检查给定名称的 CoreValue 是否存在。如果没有创建 CoreValue。将核心价值(创建/发现)与用户相关联。
Add a method in your user model:
The
assign_core_value
method meets requirement 1,2 and returns the core value assigned to the user (with the given name).Now you can do the following:
Note 1
Method logic is as follows:
1) Checks if the CoreValue is already associated with the user. If so no action is taken, core value is returned.
2) Checks if the CoreValue with the given name exists. If not creates the CoreValue. Associates the core value(created/found) with the user.
Active Record 已经给了你一个方法。在您的情况下,
您还可以通过这种方式传递多个对象。将创建所有关联,
检查此更多信息
Active Record already gives you a method. In your case,
You can also pass a number of objects at ones this way. All the associations will be created
Check this for more information