【问题标题】:Multiple Joins of ActiveRecord in rails with condition有条件的轨道中 ActiveRecord 的多个连接
【发布时间】:2013-04-02 01:18:16
【问题描述】:

我的活动记录如下:

    class TemplateItem < ActiveRecord::Base
     attr_accessible :TemplateGroup_id, :name, :object_type
     belongs_to :TemplateGroup
    end

    class TemplateGroup < ActiveRecord::Base 
     attr_accessible :name, :style, :Widget_id
     belongs_to :Widget
     has_many :TemplateItem
    end

    class Widget < ActiveRecord::Base
      attr_accessible :name, :style
      has_many :TemplateGroup
    end

我的查询如下:

templateItems=TemplateItem.joins(:TemplateGroup => :Widget).where(:Widget => {:id => w.id})

但它显示错误。

请帮助我如何编写基于小部件 id 检索数据的条件。

【问题讨论】:

  • 你想得到某个Widget的所有template_items

标签: ruby-on-rails activerecord join conditional-statements


【解决方案1】:

如果我理解,您想获取属于某个Widget 的所有template_items 和一些w.id“通过”TemplateGroups

@templateItems=TemplateItem.joins(:templateGroups)
                           .where("templateGroups.widget_id = ?", w.id)

【讨论】:

    猜你喜欢
    • 2019-10-18
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    相关资源
    最近更新 更多