摘要: 基本上,我希望用户输入分隔符的标题来筛选分布列表。一个发行版有一个配置文件,但一个合作伙伴有很多发行版。所以,如果有人将一个合作伙伴标题放入搜索表单中。这将对应于多个分配ID(通过多个matching_profile ID)。通过多个模型筛选故障
我应该描述问题的三种型号:
distribution:
belongs_to :matching_profile, :counter_cache => true
matching_profile:
belongs_to :partner
partner:
has_many :matching_profiles
我基本上需要通过分销模式访问partner.title。这里 ,有效的,是我想要做什么:
我能做到这一点的SQL: select d.* from distributions d join matching_profiles m on d.matching_profile_id = m.id join partners p on m.partner_id = p.id where p.title in ('TITLE')
/总结
更新。虽然有一些缺口......请帮忙!
在我的控制器我有:
@search = Distribution.workflow.order(sort_column + " " + sort_direction).search(params[:traits_searchable_search])
视图
我:
<%= form_for @search, :url => url_for(:controller => params[:controller], :action => params[:action]), :html => {id => "distribution_workflow",:method => :get} do |f| %>
<div class="form-block mrl mbm mtm">
<%= f.label 'matching_profile.partner.title_equal', "Partner" %>
<%= f.select @search.where('matching_profile_id in (select id from matching_profiles where partner_id in (select id from partners where title in (?)))'), Partner.select(:title), :include_blank => " " %>
</div>
的f.select部分是什么,是不正确,但我认为的想法是什么我想要做的就是在这里拍摄。我只是不知道如何去做。 /更新
我正在网页上呈现一个报告,我需要允许用户根据合作伙伴的标题筛选出哪些行可用。然而,合作伙伴的标题并不在我创建表单的模型中。表单用于分发模型,该模型引用了match_profile,该文档对partner有参考。合作伙伴表格是包含我想过滤的标题的表格。
目前,我试图通过添加另一个search_scope
search_scopes :equal => [:status, 'matching_profile.partner.title'] #obviously very wrong
更新来实现:我也试过这样:
search_scopes :equal => [:status, 'distributions.matching_profile.partner.title']
这不会给我一个错误,但不会过滤掉无论是。在正确方向迈出的一步? /更新
但我得到一个错误,因为我似乎无法“跳”两个表来获得标题。我看到这个错误消息:
AND (matching_profile.partner.title = 'PARTNER_TITLE')
当然
,这是错误的,因为没有matching_profile场,只有matching_profile_id。
我不确定最好的方法是什么。任何建议表示赞赏。
你的模型中是否有任何[':through'](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)关系? – sarnold 2011-12-29 05:17:21
否':通过'。只是':belongs_to' – Ramy 2011-12-29 13:44:24
对':through'关键字做了一点研究 - 看起来像我需要的东西走向另一个方向,即':belongs_to'上的快捷键而不是':has_many' – Ramy 2011-12-29 15:22:48