Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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 通过关联模型中的多个参数查找记录的条件在哪里?_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 通过关联模型中的多个参数查找记录的条件在哪里?

Ruby on rails 通过关联模型中的多个参数查找记录的条件在哪里?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我想通过表单中传递的多个参数查找关联的模型记录,以创建与产品模型关联的用户产品 在控制器中创建方法: @user_product = UserProduct.new(user_product_params) @product = Product.where(style_id: @user_product.style_id).where(size: @user_product.size).where(color: @user_product.color).where(country: @u

我想通过表单中传递的多个参数查找关联的模型记录,以创建与产品模型关联的用户产品

在控制器中创建方法:

  @user_product = UserProduct.new(user_product_params)
  @product = Product.where(style_id: @user_product.style_id).where(size: @user_product.size).where(color: @user_product.color).where(country: @user_product.country)
  @user_product.product_id = @product.id
型号:

  belongs_to :category, foreign_key: :category_id, class_name: "Category"
  belongs_to :style, foreign_key: :style_id, class_name: "Style"
  belongs_to :user_products
  has_one :product
  has_many :orders
产品:

  belongs_to :category, foreign_key: :category_id, class_name: "Category"
  belongs_to :style, foreign_key: :style_id, class_name: "Style"
  belongs_to :user_products
  has_one :product
  has_many :orders
用户产品:

  belongs_to :category, foreign_key: :category_id, class_name: "Category"
  belongs_to :style, foreign_key: :style_id, class_name: "Style"
  belongs_to :user_products
  has_one :product
  has_many :orders
表格通过: 尺寸、颜色、样式、类别

我不断得到错误:

undefined method `id' for #<Product::ActiveRecord_Relation:x> Did you mean? ids
未定义的#的方法'id'是什么意思?身份证
除了产品id之外,所有的东西都通过了


如何使用表单中传递的多个参数查找产品id?

其中
返回一个数组记录,但您只查找一条记录。您可以使用
find_by
返回与条件匹配的第一条记录(如果没有找到,则返回nil):


其中
返回一个数组记录,但您只查找一条记录。您可以使用
find_by
返回与条件匹配的第一条记录(如果没有找到,则返回nil):


从错误消息中可以清楚地看到您的问题。作为#的
未定义的方法“id”,您的意思是?ids
表示,您没有一个产品具有
id
,而是多个产品的关系。因此,您可能需要尝试:
@product.first.id

从错误消息中可以清楚地看到您遇到的问题。作为#的
未定义的方法“id”,您的意思是?ids
表示,您没有一个产品具有
id
,而是多个产品的关系。因此,您可能希望尝试:
@product.first.id

当您使用where查询时,它将返回ActiveRecord::Relation,即使返回单个记录,它也是数组形式。因此,@product变量的结果是数组形式的,如果您确定从这个查询中只能得到一条记录,那么应该使用
。使用
方法,但我建议您使用
。通过
方法查找记录。

因为您使用where query,它将返回ActiveRecord::Relation,它是数组形式的,即使它返回单个记录。因此,@product变量的结果是数组形式的,如果您确定从这个查询中只会得到一条记录,那么您应该使用
。使用
方法,但我建议您使用
。通过
方法查找记录。

where
子句返回
ActiveRecord
对象在
ActiveRecord\u关系中
是一种
数组
,即使只有一条记录。试试这个

@user_product = UserProduct.new(user_product_params)
@products = Product.where(style_id: @user_product.style_id, size: @user_product.size, color: @user_product.color, country: @user_product.country)
@user_product.product_id = @products.first.id

希望这对你有帮助

where
子句返回
ActiveRecord
关系中的对象,该关系是一种
数组
,即使只有一条记录。试试这个

@user_product = UserProduct.new(user_product_params)
@products = Product.where(style_id: @user_product.style_id, size: @user_product.size, color: @user_product.color, country: @user_product.country)
@user_product.product_id = @products.first.id

希望这对你有帮助

其中条件在数组中给出结果

试试这个

@product.first.id

其中条件在数组中给出结果

试试这个

@product.first.id

谢谢好吧,那就行了。然而,让我困惑的是,每个记录都是唯一的,因为产品模型中的任何记录都不会具有与产品模型中的另一个记录匹配的所有属性。因此,不应该。在哪里找到一个,而且只需要不需要先使用?或者我遗漏了一些东西,如果是的话,你能解释一个BIT吗?如果你使用
。where()
子句来查找一个实体,你通常不会只有一个与搜索匹配的对象,而是多个。这就是返回
ActiveRecord\u关系的原因。当您事先知道将只返回一个可能的元素时,您应该使用已建议的@max.@uno您还应该为该唯一性要求设置模型验证和数据库约束谢谢。好吧,那就行了。然而,让我困惑的是,每个记录都是唯一的,因为产品模型中的任何记录都不会具有与产品模型中的另一个记录匹配的所有属性。因此,不应该。在哪里找到一个,而且只需要不需要先使用?或者我遗漏了一些东西,如果是的话,你能解释一个BIT吗?如果你使用
。where()
子句来查找一个实体,你通常不会只有一个与搜索匹配的对象,而是多个。这就是返回
ActiveRecord\u关系的原因。当您事先知道将只返回一个可能的元素时,您应该使用已建议的@max@uno。您还应该为该唯一性要求设置模型验证和数据库约束