Ruby-on-rails Rails - 如何使用查找或创建
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5733222/
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 - How to use Find Or Create
提问by AnApprentice
I have the following:
我有以下几点:
@permission = @group.permissions.create(
:user_id => @user.id,
:role_id => 2,
:creator_id => current_user.id)
How can I update that to be find_or_create, so that if this record already exists, it's assigned to @permission, and if it doesn't exist, the record is created?
如何将其更新为find_or_create,以便如果此记录已存在,则将其分配给@permission,如果不存在,则创建该记录?
回答by Gavin Miller
While the accepted answer is correctit's important to note that in Rails 4 this syntax will be changing (and the hash syntax). You should be writing the following:
虽然接受的答案是正确的,但重要的是要注意,在 Rails 4 中,此语法将发生变化(以及哈希语法)。您应该编写以下内容:
@permission = Permission.where(
user_id: @user.id,
role_id: 2,
creator_id: current_user.id).first_or_create
Which actually looks much closer to your original method! See the sub-section Deprecated Findersfor more details.
这实际上看起来更接近您的原始方法!有关更多详细信息,请参阅已弃用的 Finders小节。
回答by fl00r
Related topic:
相关主题:
find_or_create_by in Rails 3 and updating for creating records
You can extend ActiveRecord with your own update_or_createmethod (see related topic) and then you can use this
您可以使用自己的update_or_create方法扩展 ActiveRecord (请参阅相关主题),然后您可以使用它
@permission = Permission.update_or_create_by_user_id_and_role_id_and_creator_id(@user.id, 2, current_user.id) do |p|
p.group_id = @group.id
end
Or you can use find_or_create_by...method:
或者你可以使用find_or_create_by...方法:
@permission = Permission.find_or_create_by_user_id_and_role_id_and_creator_id(@user.id, 2, current_user.id)
@permission.group = @group
@permission.save
回答by Blair Anderson
I'm updating questions with versioned answers. Because its important.
我正在用版本化的答案更新问题。因为它很重要。
Rails 4 (docs)
Rails 4(文档)
There are a few ways to "find or create" an object, one is find_or_create_by(args)
有几种方法可以“查找或创建”一个对象,一种是 find_or_create_by(args)
Client.find_or_create_by(email: "[email protected]", phone: "4255551212")
But the community preferredwayis using where
但社区首选的方式是使用where
client = Client.where(email: "[email protected]", phone: "4255551212").first_or_create
client = Client.where(email: "[email protected]", phone: "4255551212").first_or_create
and then you can do something like:
然后你可以做这样的事情:
client = Client.where(client_params.slice(:email, :phone)).first_or_create
client.update(client_params)
Rails 3 (docs)
Rails 3(文档)
Suppose you want to find a client named ‘Andy', and if there's none, create one and additionally set his locked attribute to false. You can do so by running:
假设您要查找名为“Andy”的客户端,如果没有,则创建一个并将其锁定属性设置为 false。您可以通过运行:
client = Client.where(:first_name => 'Andy').first_or_create(:locked => false)
# => #<Client id: 1, first_name: "Andy", orders_count: 0, locked: false, created_at: "2011-08-30 06:09:27", updated_at: "2011-08-30 06:09:27">
回答by lkahtz
Or you wanna try this if you have many fields to fill in:
或者,如果您有很多字段要填写,您想试试这个:
conditions = { :user_id => @user.id,
:role_id => 2,
:creator_id => current_user.id }
@permission = group.permissions.find(:first, :conditions => conditions) || group.permissions.create(conditions)
see this post: How can I pass multiple attributes to find_or_create_by in Rails 3?

