`
lzqustc
  • 浏览: 207158 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

Activerecord应用实例——多表关联查询

阅读更多

 

关系:

class Company < ActiveRecord::Base

  has_many :regist_infos

end

class RegistInfo < ActiveRecord::Base

   belongs_to:company           company_id

   belongs_to:b2bsite             b2bsite_id

   has_many:post_infos                

end

class PostInfo < ActiveRecord::Base

   belongs_to:regist_info          regist_info_id

   belongs_to:product            product_id    

end

class Product < ActiveRecord::Base

   belongs_to :company          company_id

   has_many:post_infos                       

end

 

class B2bsite < ActiveRecord::Base

   has_many:regist_infos                  

end

<!--EndFragment-->

 

 

应用以上这些关系,简化查询语句:

- @company.regist_infos.each do |info|

   %table

     %tr

        %td{:width=>"16%", :align=>"center", :valign=>"middle"}

           = B2bsite.find( info.b2bsite_id ).name

        %td{:width=>"20%",:align=>"left",:valign=>"middle"}

           = "已注册"            if info.regist_state == 1 

           = "已通过邮箱验证"    if info.regist_state == 2 

           = "已完成"            if info.regist_state == 3 

        %td{:width=>"15%",:align=>"left",:valign=>"middle"}

           = info.posted_product

        %td{:width=>"15%",:align=>"left",:valign=>"middle"}

           = info.posted_info 

        %td{:width=>"20%",:align=>"left",:valign=>"middle"}

           - info.post_infos.each do |p| 

              = Product.find( p.product_id ).title     if p.post_type == 2

              %br

        %td{:width=>"20%",:align=>"left",:valign=>"middle"}              

           - info.post_infos.each do |p| 

              = Product.find( p.product_id ).name    if p.post_type == 1

              %br

<!--EndFragment-->
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics