Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 通过返回Nil而不是创建记录来确定作用域的find_或create__Ruby On Rails_Ruby - Fatal编程技术网

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