Ruby on rails 通过返回Nil而不是创建记录来确定作用域的find_或create_
而不是创建记录,Ruby on rails 通过返回Nil而不是创建记录来确定作用域的find_或create_,ruby-on-rails,ruby,Ruby On Rails,Ruby,而不是创建记录,find\u或\u create\u by只是返回nil。我不知道为什么 我正在这样做: current_store.jet.find_or_create_by(seller_id: seller_id) do |credentials| credentials.update_attributes( marketplace: marketplace, seller_id: seller_id, auth_token: auth_token) end 它返回这个,
find\u或\u create\u by
只是返回nil
。我不知道为什么
我正在这样做:
current_store.jet.find_or_create_by(seller_id: seller_id) do |credentials|
credentials.update_attributes(
marketplace: marketplace,
seller_id: seller_id,
auth_token: auth_token)
end
它返回这个,而不是出于某种原因创建记录:
*** NoMethodError Exception: undefined method `find_or_create_by' for nil:NilClass
我检查了所有的论点,它们都是正确的。但是肯定还没有jet
实例,但这就是重点
我的Jet表如下所示:
create_table "jets", force: :cascade do |t|
t.text "auth_token"
t.text "marketplace"
t.integer "store_id"
t.boolean "three_speed"
t.text "seller_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
我的current_store
方法如下所示,并返回正确的存储
def current_store
@current_store ||= Store.find(session[:fba_shipping_id])
end
我在哪里迷路了
编辑:
下面是关于这些关系的更多信息
class Store < ActiveRecord::Base
has_one :jet
end
类存储
及
class-Jet
既然您有一个有一个关联,那么编写如下方法可能会更清晰:
Jet.find_or_create_by(store_id: current_store.id) do |jet|
jet.seller_id = seller_id
jet.marketplace = marketplace
jet.auth_token = auth_token
end
在我看来,这将完成你想要做的事情,但以更标准的方式
请记住,这不会更新现有jet
的属性(seller\u id
,marketplace
,auth\u token
),但如果current\u store
尚未分配jet
,则会使用这些属性创建一个
即使记录已经存在,也要更新这些属性吗
既然你有一个有一个关联,那么按照如下方式编写方法可能会更清晰:
Jet.find_or_create_by(store_id: current_store.id) do |jet|
jet.seller_id = seller_id
jet.marketplace = marketplace
jet.auth_token = auth_token
end
在我看来,这将完成你想要做的事情,但以更标准的方式
请记住,这不会更新现有jet
的属性(seller\u id
,marketplace
,auth\u token
),但如果current\u store
尚未分配jet
,则会使用这些属性创建一个
即使记录已经存在,也要更新这些属性吗
您当前的问题是
current_store.jet
返回nil
,从而返回NoMethodError
如果一家商店只有一个Jet
,那么store
也只能有一个Jet
的卖家
has_one
()提供了构建器方法build_association
和create_association
,因此您可以使用
current_store.create_jet(marketplace: marketplace,
seller_id: seller_id,
auth_token: auth_token)
但是,您可能需要确保存储
中没有喷射
,例如
@jet = current_store.jet || current_store.create_jet(marketplace: marketplace,
seller_id: seller_id,
auth_token: auth_token)
如果@HeliosdeGuerra在评论中指出这是您的意图,则不会更新现有的喷气式飞机。如果您真的想使用这些方法并更新现有的Jet
,您可以选择一些迟钝的方法,如:
@jet = current_store.jet || current_store.create_jet(seller_id: seller_id)
@jet.update_attributes(marketplace: marketplace,auth_token: auth_token)
但是,在这两种情况下,您都应确保喷气式飞机已通过验证,例如:
# Example 1
@jet = current_store.jet || current_store.create_jet(marketplace: marketplace,
seller_id: seller_id,
auth_token: auth_token)
if @jet.persisted?
# passed validation or already existed
else
# failed validation
end
# Example 2
@jet = current_store.jet || current_store.create_jet(seller_id: seller_id)
if @jet.update_attributes(marketplace: marketplace,auth_token: auth_token)
# passed validation
else
# failed validation
end
你现在的问题是
current_store.jet
返回nil
,从而返回NoMethodError
如果一家商店只有一个Jet
,那么store
也只能有一个Jet
的卖家
has_one
()提供了构建器方法build_association
和create_association
,因此您可以使用
current_store.create_jet(marketplace: marketplace,
seller_id: seller_id,
auth_token: auth_token)
但是,您可能需要确保存储
中没有喷射
,例如
@jet = current_store.jet || current_store.create_jet(marketplace: marketplace,
seller_id: seller_id,
auth_token: auth_token)
如果@HeliosdeGuerra在评论中指出这是您的意图,则不会更新现有的喷气式飞机。如果您真的想使用这些方法并更新现有的Jet
,您可以选择一些迟钝的方法,如:
@jet = current_store.jet || current_store.create_jet(seller_id: seller_id)
@jet.update_attributes(marketplace: marketplace,auth_token: auth_token)
但是,在这两种情况下,您都应确保喷气式飞机已通过验证,例如:
# Example 1
@jet = current_store.jet || current_store.create_jet(marketplace: marketplace,
seller_id: seller_id,
auth_token: auth_token)
if @jet.persisted?
# passed validation or already existed
else
# failed validation
end
# Example 2
@jet = current_store.jet || current_store.create_jet(seller_id: seller_id)
if @jet.update_attributes(marketplace: marketplace,auth_token: auth_token)
# passed validation
else
# failed validation
end
如果看不到Store
和Jet
之间的关联定义,很难判断,但是如果Store
有很多:Jet
,那么您的语句中不应该有一个复数的jets
?(例如,current\u store.jets.find\u或\u create\u by
)@HeliosDegure是个好主意,我更新了OP,但是store
有一个Jet
和Jet
属于商店啊,在有一个上语法有点不同。您必须使用build\u other
方法。尝试当前\u商店。构建\u jet。查找\u或\u创建\u by
…拍摄。。返回的NoMethodError(未定义的方法find\u或\u create\u by'for#):`当我这样做current\u store.build\u jet.find\u或\u create\u by(seller\u id:seller\u id)
时,如果看不到store
和jet
之间的关联定义,就很难判断了,但是如果store
有很多:jet
,那么,您的声明中不应该有一个多元化的jets
?(例如,current\u store.jets.find\u或\u create\u by
)@HeliosDegure是个好主意,我更新了OP,但是store
有一个Jet
和Jet
属于商店啊,在有一个上语法有点不同。您必须使用build\u other
方法。尝试当前\u商店。构建\u jet。查找\u或\u创建\u by
…拍摄。。返回的NoMethodError(未定义的方法find\u或\u create\u by'for\35;):`当我这样做current\u store.build\u jet.find\u或\u create\u by(seller\u id:seller\u id)
时,这不会更新现有jet
的属性。。。目前还不清楚@ToddT是否希望在jet已经存在的情况下更新这些属性。从字段判断,他可能不想更新这些字段,但是他最初编写方法的方式,我认为他确实想更新字段。@HeliosdeGuerra更新了,老实说,我会使用你的解决方案,但我不确定如果商店id
存在会发生什么,但卖家id
不存在,因为这会创建2