小编May*_*tel的帖子

轨道4中的多级连接

我想在rails 4中进行此查询

select r.region_id, r.region_name from countries c, zones z, regions r where c.country_id = $country_id (pass as parameter) and c.country_id = z.zone_id and z.subzone_id = r.region_id
Run Code Online (Sandbox Code Playgroud)

楷模 :

 #Country.rb
class Country < ActiveRecord::Base
  has_one :place, foreign_key: :place_id
  has_many :zones , foreign_key: :place_id
  has_many :subzones, :through => :zones
end

#Zone.rb
class Zone < ActiveRecord::Base
 belongs_to :place
 belongs_to :subzone, :class_name => 'Place' , :foreign_key => :subzone_id
end

#Region.rb
class Region < ActiveRecord::Base
  has_one :place , foreign_key: :place_id
end

#Place.rb
class Place …
Run Code Online (Sandbox Code Playgroud)

join ruby-on-rails

2
推荐指数
1
解决办法
2291
查看次数

标签 统计

join ×1

ruby-on-rails ×1