关系:
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
应用以上这些关系,简化查询语句:
- @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