Rails加入并包含多个表

时间:2018-03-25 07:57:05

标签: activerecord ruby-on-rails-5

我有以下型号:

class Vendor < ApplicationRecord
  has_many :warehouse_ingredients
  has_many :warehouse_ingredient_logs
end


class WarehouseIngredient < ApplicationRecord
  belongs_to :vendor
end

class WarehouseIngredientLog < ApplicationRecord
  belongs_to :vendor
end

我想要WarehouseIngredient&amp;的所有记录。 WarehouseIngredientLog表,其中vendor_id = 1?

编辑1

以下是WarehouseIngredient

的数据
# id, warehouse_id, ingredient_id, vendor_id, quantity, cost, lot_number, created_at, updated_at
'1', '1', '1', '1', '48.00000000', '0.84800000', 'ip201803241', '2018-03-24 14:07:17', '2018-03-24 14:07:17'
'2', '1', '2', '1', '13.00000000', '1.39300000', 'ip201803241', '2018-03-24 14:07:17', '2018-03-24 14:07:17'
'3', '1', '3', '1', '43.00000000', '1.50200000', 'ip201803241', '2018-03-24 14:07:17', '2018-03-24 14:07:17'
'4', '1', '4', '1', '12.00000000', '0.36100000', 'ip201803241', '2018-03-24 14:07:17', '2018-03-24 14:07:17'
'25', '1', '1', '1', '35.00000000', '1.00000000', 'ip2018032510', '2018-03-25 08:11:17', '2018-03-25 08:11:17'
'26', '1', '2', '1', '41.00000000', '1.00000000', 'ip2018032510', '2018-03-25 08:11:17', '2018-03-25 08:11:17'
'27', '1', '3', '1', '13.00000000', '1.00000000', 'ip2018032510', '2018-03-25 08:11:17', '2018-03-25 08:11:17'
'28', '1', '4', '1', '50.00000000', '1.00000000', 'ip2018032510', '2018-03-25 08:11:17', '2018-03-25 08:11:17'

此表中的条目将随着成分被移除(删除行)或添加(添加新行)而增加或减少。此外,一种成分可能同时具有多个条目,具有不同的lot_numbers。

来自WarehouseIngredientLog的

# id, warehouse_id, ingredient_id, vendor_id, bill_id, quantity, cost, lot_number, transaction_type, created_by_id, updated_by_id, created_at, updated_at
'1', '1', '1', '1', '1', '48.00000000', '0.84800000', 'ip201803241', '0', '1', '1', '2018-03-24 14:07:17', '2018-03-24 14:07:17'
'2', '1', '2', '1', '1', '13.00000000', '1.39300000', 'ip201803241', '0', '1', '1', '2018-03-24 14:07:17', '2018-03-24 14:07:17'
'3', '1', '3', '1', '1', '43.00000000', '1.50200000', 'ip201803241', '0', '1', '1', '2018-03-24 14:07:17', '2018-03-24 14:07:17'
'4', '1', '4', '1', '1', '12.00000000', '0.36100000', 'ip201803241', '0', '1', '1', '2018-03-24 14:07:17', '2018-03-24 14:07:17'
'25', '1', '1', '1', '10', '35.00000000', '1.00000000', 'ip2018032510', NULL, '1', '1', '2018-03-25 08:11:17', '2018-03-25 08:11:17'
'26', '1', '2', '1', '10', '41.00000000', '1.00000000', 'ip2018032510', NULL, '1', '1', '2018-03-25 08:11:17', '2018-03-25 08:11:17'
'27', '1', '3', '1', '10', '13.00000000', '1.00000000', 'ip2018032510', NULL, '1', '1', '2018-03-25 08:11:17', '2018-03-25 08:11:17'
'28', '1', '4', '1', '10', '50.00000000', '1.00000000', 'ip2018032510', NULL, '1', '1', '2018-03-25 08:11:17', '2018-03-25 08:11:17'

此表中的条目将包含所有数据记录,因此此处将记录以前表值的任何更改。

我想要的结果是获取供应商1成分1的数据,其中transaction_type = 0;

0 个答案:

没有答案