访问连接表与ActiveRecord Rails中的许多通过关联

时间:2015-03-20 08:43:46

标签: ruby-on-rails rails-activerecord

我有

class Request < ActiveRecord::Base
  has_many   :operations 
end 

class Operation < ActiveRecord::Base
  has_many   :goods, through: :positions
end

class Good < ActiveRecord::Base
  has_many   :operations, through: :positions
end

我想要
Request.where(goods: {code: '123'}(在元代码中)

评论
我知道我可以这样查询:
Request.includes(:operations).where(operations: {kind: 'acceptance'})
但我无法弄清楚如何使用正确的includes来查询商品。

已更新:架构提取

create_table "requests", force: :cascade do |t|
end

create_table "operations", force: :cascade do |t|
  t.string   "kind"
  t.integer  "request_id"
end

create_table "goods", force: :cascade do |t|
  t.decimal  "purchase_price", precision: 12, scale: 2
end

create_table "positions", force: :cascade do |t|
  t.integer  "good_id"
  t.integer  "request_id"
  t.integer  "operation_id"
end

0 个答案:

没有答案