rails 4找到包括

时间:2014-03-03 10:31:17

标签: ruby-on-rails ruby ruby-on-rails-4

我有这样的关系:

class Organization < ActiveRecord::Base
  belongs_to :organization_type
  has_many :organization_type_links
end


class OrganizationType < ActiveRecord::Base
  has_many :organizations
  has_many :organization_type_links
end


class OrganizationTypeLink < ActiveRecord::Base
  belongs_to :organization
  belongs_to :organization_type
end

在表组织中我有字段:organization_type_link_id 在表organization_type_links:organization_type_id

我怎样才能选择所有组织,在表organization_type_links中,organization_type_id是什么价值?在轨道3我知道这样做......

在第四次尝试:

Organization.includes(:organization_type_links).where(organization_type_id: params[:id])

但是这并没有取得我想要的东西......

1 个答案:

答案 0 :(得分:1)

请尝试joins

Organization.joins(:organization_type_links).where(organization_type_links: { organization_type_id: params[:id] })