【发布时间】:2012-01-29 15:36:04
【问题描述】:
总结: 基本上,我希望用户输入合作伙伴的标题以过滤分布列表。一个发行版有一个配置文件,但一个合作伙伴有多个发行版。因此,如果有人将一个合作伙伴头衔放入搜索表单中。这将对应于多个分布 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 部分是不正确的,但我认为我正在尝试做的想法在这里被捕获。我只是不知道该怎么做。 /更新
我正在网页中展示一份报告,我需要允许用户根据合作伙伴的标题过滤掉哪些行可用。然而,合作伙伴的头衔不在我为其创建表单的模型中。该表单用于分发模型,它引用了matching_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关系吗? -
没有
:throughs。只是:belongs_to -
对
:through关键字进行了一些研究——看起来我需要一些相反的东西,即:belongs_to上的快捷方式而不是:has_many -
感谢更完整的模型描述;希望这足以让某人提出最干净的方法。我想建议将逻辑从您的视图中移到您的控制器中,但这实际上无助于解决问题。
标签: ruby-on-rails ruby-on-rails-3 model-view-controller model