我有
@total = Purchase::Total.find(1);
Total model have:
has_many :items
belongs_to :member
belongs_to :company
..................
Also companies model has
has_many :addresses
has_one :subscription
..................
还有更多
如何从包含所有@total object
,has_one
依赖项的belongs_to
中获取树?
即
<Purchase::Total id: 3, member_id: 4, created_at: \"2015-11-25 14:47:46\", updated_at: \"2015-11-25 14:47:46\", affiliate_company_id: nil, is_paid: false, currency: 1, company_id: 37020, ser_id: 2>
<Company id: 37020, name: \"Andrew\", parent_id: 37019, member_company_id: 37019, payment_company_id: 37019, widget_id: 3003359>
所以......(我用@ total.inspect和@ total.company.inspect做了例子),我需要像inspect
这样的东西自动返回所有对象。
答案 0 :(得分:0)
Queue
和Hash
并添加Total
(型号名称)。tablize
名称,在哈希中创建一个新条目,并添加相关模型的tablize
d名称。最后,您的哈希应该如下所示:
{ total: { company: [ :subscription, :addresses ] }, items: { associated: { another: :another_one } } }
然后您可以在查询中使用它:
Total.where().join(hash[:total])
它也将获取所有相关数据。然后,您可以简单地遍历属性。如果属性类型为ActiveRecord
(或类似),则其为关联的模型数据。