轨道4,通过的has_many协会 - 寻找相关的对象 [英] Rails 4, has_many through association - finding associated objects

查看:223
本文介绍了轨道4,通过的has_many协会 - 寻找相关的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么 Item.where(类别:X)是不是为我工作。我希望这个语句返回这是X类分类下的所有项目。请参考下面我的协会:

 类类别< ActiveRecord的::基地
    的has_many:分类已
    的has_many:项目:通过=> :分类已
结束类项目< ActiveRecord的::基地
    的has_many:分类已
    的has_many:类别:通过= GT; :分类已
结束等级分类和LT; ActiveRecord的::基地
    belongs_to的:项目
    belongs_to的:类别
结束

Category.find(1).items 返回该类别的所有项目。请参阅下面的byebug控制台输出这表明我的观点进一步。这似乎是所生成正在寻找在项目表中的一个类别列在SQL - 这当然不存在。任何想法?

  1:类ItemsController< ApplicationController中
    2:#返回项的完整列表
    3:DEF指数
    4:@items = Item.all
    5:byebug
= GT; 6:结束
    7:(byebug)Item.where(类别:1)
  项目负载(0.6ms)选择项目。* FROM项目WHERE项目。类别= 1
#<项目:: ActiveRecord_Relation:0x007fb5d1a37f08>
(byebug)Category.find(1).items
  类别负荷(为0.7m​​s)选择类别。* FROM类别WHERE类别,ID= $ 1 LIMIT 1 [身份证,1]
  项目负载(1.7ms)选择项目。* FROM项目INNER JOIN分类已ON项目,ID=分类已。ITEM_IDWHERE分类已。CATEGORY_ID= $ 1 [[ CATEGORY_ID,1]
#<的ActiveRecord ::协会:: CollectionProxy [#<项目编号:1​​,标题:华丽棉裤介绍:悲格言suscipit引渡cupiditate quia officiis ......,价格:73960,状态:0, published_date:2016年7月14日5时35分49秒,created_at:2016年7月17日5点15分07秒,的updated_at:2016年7月17日5点15分07秒,seller_id:1>中#<项目编号:5,标题:光滑的大理石鞋,说明:魁mollitia股癣魁placeat Reiciendis EA小号......,价格:35146,状态:0,published_date:2016年7月14日05 :45:02,created_at:2016年7月17日5时15分07秒,的updated_at:2016年7月17日5时15分07秒,seller_id:1>中#<项目编号:7,标题:土气混凝土灯,说明:坐奥迪奥非exercitationem Atque非sapiente武......,价格:82016,状态:2,published_date:2016年7月13日00:00:00开始,created_at: 2016年7月17日5时15分07秒,的updated_at:2016年7月17日5时15分07秒,seller_id:1>中#<项目编号:1​​0,标题:真棒木表,说明: Possimus consequatur法无Quidem molestiae VOLUP ......,价格:59519,状态:2,published_date:2016年7月9日00:00:00,created_at:2016年7月17日五时15分07秒 ,的updated_at:2016年7月17日5时15分07秒,seller_id:1>中#<项目编号:1​​2,标题:轻集料混凝土袋介绍:阿梅德ullam assumenda eligendi consectetur quae。 BL ...,价格:72081,状态:2,published_date:2016年7月16日00:00:00开始,created_at:2016年7月17日5时15分07秒,的updated_at:2016-07 -17 5时15分07秒,seller_id:2,#<项目编号:1​​3,标题:平庸的塑料电脑,说明:Excepturi MODI EST非魁iusto。 Molestiae offici ......,价格:94357,状态:2,published_date:2016年7月15日00:00:00开始,created_at:2016年7月17日5时15分07秒,的updated_at:2016- 07-17 5时15分07秒,seller_id:2,#<项目编号:1​​5,标题:不可思议的塑料袋介绍:威赛voluptas ducimus soluta atque voluptatem EUM。 ...,价格:15661,状态:2,published_date:2016年7月14日00:00:00开始,created_at:2016年7月17日5时15分07秒,的updated_at:2016-07- 17 5时15分07秒,seller_id:2,#<项目编号:1​​6,标题:轻铁手表,说明:标识亮片rerum悲静坐必须遵守尼莫laborum。 OMNIS ......,价格:65306,状态:4,published_date:2016年7月11日00:00:00开始,created_at:2016年7月17日5时15分07秒,的updated_at:2016-07 -17 5时15分07秒,seller_id:1>中#<项目编号:1​​7,标题:土气亚麻椅子,说明:Explicabo魁广告虚无。 Voluptatem placeat autem。 ...,价格:39752,状态:4,published_date:2016年7月4日00:00:00开始,created_at:2016年7月17日5时15分07秒,的updated_at:2016-07- 17 5时15分07秒,seller_id:1>中#<项目编号:1​​8,标题:平庸的铜车,说明:负魁UT EST非维罗saepe。魁SED权交易人诉...,价格:87765,状态:4,published_date:2016年7月5日00:00:00开始,created_at:2016年7月17日5时15分07秒,的updated_at: 2016年7月17日5点15分07秒,seller_id:1 GT;]≥


解决方案

我想通过的读取与的has_many关联的对象的办法是做这样的事情:

  Item.joins(:类)。凡(类:{ID:1})

我还没有找到一个这样做的更好的方法。

I'm wondering why Item.where(category: x) isn't working for me. I expect this statement to return all items which are categorized under category x. Please see below my associations:

class Category < ActiveRecord::Base
    has_many :categorizations
    has_many :items, :through => :categorizations
end

class Item < ActiveRecord::Base
    has_many :categorizations
    has_many :categories, :through => :categorizations
end

class Categorization < ActiveRecord::Base
    belongs_to :item
    belongs_to :category
end

Category.find(1).items returns all items for that category. Please see below, the byebug console output which demonstrates my point further. It seems like the SQl that is generated is looking for a category column in the Item table - which of course doesn't exist. Any ideas?

    1: class ItemsController < ApplicationController
    2:   # Returns full list of items
    3:   def index
    4:     @items = Item.all
    5:      byebug
=>  6:   end
    7: 

(byebug) Item.where(category: 1)
  Item Load (0.6ms)  SELECT "items".* FROM "items" WHERE "items"."category" = 1
#<Item::ActiveRecord_Relation:0x007fb5d1a37f08>
(byebug) Category.find(1).items
  Category Load (0.7ms)  SELECT  "categories".* FROM "categories" WHERE "categories"."id" = $1 LIMIT 1  [["id", 1]]
  Item Load (1.7ms)  SELECT "items".* FROM "items" INNER JOIN "categorizations" ON "items"."id" = "categorizations"."item_id" WHERE "categorizations"."category_id" = $1  [["category_id", 1]]
#<ActiveRecord::Associations::CollectionProxy [#<Item id: 1, title: "Gorgeous Cotton Pants", description: "Dolor dicta suscipit aut cupiditate quia officiis ...", price: 73960, status: 0, published_date: "2016-07-14 05:35:49", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 1>, #<Item id: 5, title: "Sleek Marble Shoes", description: "Qui mollitia corporis qui placeat. Reiciendis ea s...", price: 35146, status: 0, published_date: "2016-07-14 05:45:02", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 1>, #<Item id: 7, title: "Rustic Concrete Lamp", description: "Sit odio non exercitationem. Atque non sapiente vo...", price: 82016, status: 2, published_date: "2016-07-13 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 1>, #<Item id: 10, title: "Awesome Wooden Table", description: "Possimus consequatur nulla. Quidem molestiae volup...", price: 59519, status: 2, published_date: "2016-07-09 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 1>, #<Item id: 12, title: "Lightweight Concrete Bag", description: "Amet ullam assumenda eligendi consectetur quae. Bl...", price: 72081, status: 2, published_date: "2016-07-16 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 2>, #<Item id: 13, title: "Mediocre Plastic Computer", description: "Excepturi modi est non qui iusto. Molestiae offici...", price: 94357, status: 2, published_date: "2016-07-15 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 2>, #<Item id: 15, title: "Incredible Plastic Bag", description: "Vel voluptas ducimus soluta atque voluptatem eum. ...", price: 15661, status: 2, published_date: "2016-07-14 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 2>, #<Item id: 16, title: "Lightweight Iron Watch", description: "Id sequi rerum dolor sit sunt nemo laborum. Omnis ...", price: 65306, status: 4, published_date: "2016-07-11 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 1>, #<Item id: 17, title: "Rustic Linen Chair", description: "Explicabo qui ad nihil. Voluptatem placeat autem. ...", price: 39752, status: 4, published_date: "2016-07-04 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 1>, #<Item id: 18, title: "Mediocre Copper Car", description: "Minus qui ut est non vero saepe. Qui sed quos et v...", price: 87765, status: 4, published_date: "2016-07-05 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 1>]>

解决方案

I think the approach for fetching associated objects with has_many through is to do something like:

Item.joins(:categories).where(categories: {id: 1})

I'm yet to find a better approach of doing this.

这篇关于轨道4,通过的has_many协会 - 寻找相关的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆