【问题标题】:Rails Console outputting only first record of array?Rails 控制台仅输出数组的第一条记录?
【发布时间】:2014-06-21 08:10:06
【问题描述】:

我正在使用我的 rails 控制台中的数据来测试一些关系,它似乎只输出数组的第一条记录。

我有一个名为c 的变量,它是我项目中Comparison 类的一个实例。

我建立了一个关系,因此我可以调用 c.products,它会返回一个包含该比较中所有产品的数组,在本例中为 2。

问题是当我打电话时:

c.products 它只返回 2 个产品中的第一个产品。

c.products[1] 返回零。

y c.products 同一数据的更详细版本显示了这两种产品。

c.products.count 返回 2。

这是它在 rails c 中的样子:

c.products
=> [#<Product id: 1, category_id: 3, name: "Camera", description: "erererer", brand: "Canon", quantity: 3, star_rating: 4, price: 299.0, created_at: "2014-06-18 20:26:34", updated_at: "2014-06-19 06:40:34">]

c.products.count
   (0.5ms)  SELECT COUNT(*) FROM "products" INNER JOIN "compared_products" ON "products"."id" = "compared_products"."product_id" WHERE "compared_products"."comparison_id" = $1  [["comparison_id", 2]]
=> 2

并且 y c.products 将其压缩为仅显示最后一个产品,因为返回大量数据。

y c.products
--- &10 !ruby/object:ActiveRecord::Associations::CollectionProxy
association: !ruby/object:ActiveRecord::Associations::HasManyThroughAssociation
  reflection: &1 !ruby/object:ActiveRecord::Reflection::ThroughReflection
    macro: :has_many
    name: :products
    scope: 
    options:
      :through: :compared_products
    active_record: &2 !ruby/class 'Comparison'
    klass: &3 !ruby/class 'Product'
    plural_name: products
    collection: true
    automatic_inverse_of: false
    type: 
    foreign_type: products_type
    constructable: true
    source_reflection_name: :product
    class_name: Product
    chain:
    - *1
    - !ruby/object:ActiveRecord::Reflection::AssociationReflection
      macro: :has_many
      name: :compared_products
      scope: 
      options: {}
      active_record: *2
      klass: !ruby/class 'ComparedProduct'
      plural_name: compared_products
      collection: true
      automatic_inverse_of: 
      type: 
      foreign_type: compared_products_type
      constructable: true
      class_name: ComparedProduct
      foreign_key: comparison_id
      active_record_primary_key: id
    scope_chain:
    - []
    - []
  owner: !ruby/object:Comparison
    attributes:
      id: 2
      user_id: 1
      name: Jacks Electronics Comparison
      created_at: 2014-06-20 16:12:36.828154000 Z
      updated_at: 2014-06-20 16:12:36.828154000 Z
  loaded: true
  target:
  - !ruby/object:Product
    attributes:
      id: 1
      category_id: 3
      name: Camera
      description: erererer
      brand: Canon
      quantity: 3
      star_rating: 4
      price: 299.0
      created_at: 2014-06-18 20:26:34.276570000 Z
      updated_at: 2014-06-19 06:40:34.776216000 Z

【问题讨论】:

  • 不只是日志记录吗?试试c.products[1]
  • c.products[1] 返回零。这真是太奇怪了。
  • 比较好c.products.first == c.products.last
  • c.products.first == c.products.last 返回真。我很困惑。

标签: ruby-on-rails ruby


【解决方案1】:

你可以看到c.products没有触发sql查询,这意味着它被缓存了。另一方面,c.products.count 触发查询并告诉您来自数据库的实际信息。所以你的c.products 可能通过其他关联或直接在数据库中手动更改。接收实际产品做c.products.reload,统计缓存产品(不触发sql)做c.products.size

详细解释看这里https://stackoverflow.com/a/18997294

【讨论】:

  • 非常感谢鲁斯塔姆。重新加载了 rails 控制台,它现在正在返回完整的数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多