无法通过多个模型进行过滤
概括: 基本上,我希望用户输入合作伙伴的标题来过滤发行版列表。一种发行版有一种配置文件,但一位合作伙伴有多种发行版。因此,如果有人将一个合作伙伴头衔放入搜索表单中。这将对应于多个分发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 来做: 从发行版中选择 d.* d joinmatching_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 部分是不正确的,但我认为我想要做的事情的想法在这里被捕获。我只是不知道该怎么做。 /update
我正在网页中呈现一份报告,我需要允许用户根据合作伙伴的头衔过滤出可用的行。然而,合作伙伴的头衔并不在我为其创建表单的模型中。该表单用于分配模型,其中引用了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']
这不会给我错误,但也不会过滤。朝着正确方向迈出的一步? /update
但我收到错误,因为我似乎无法“跳跃”两个表来到达标题。我在错误消息中看到了这一点:
AND (matching_profile.partner.title = 'PARTNER_TITLE')
当然,这是错误的,因为没有matching_profile字段,只有matching_profile_id。
我不确定解决这个问题的最佳方法是什么。任何建议表示赞赏。
summary:
Basically, I want the user to input the title of a parter to filter a list of distributions on. One distribution has one profile, but one partner has many distributions. So, if someone were to put one partner title into the search form. That would correspond to multiple distribution ids (by way of multiple matching_profile ids).
I should describe the three models in question:
distribution:
belongs_to :matching_profile, :counter_cache => true
matching_profile:
belongs_to :partner
partner:
has_many :matching_profiles
I basically need to access partner.title through the distribution model.
i can do it in 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' )
/summary
UPDATE:
here, effectively, is what I'd like to do. There are some gaps missing though...Please help!
in my controller i have:
@search = Distribution.workflow.order(sort_column + " " + sort_direction).search(params[:traits_searchable_search])
in the view I have:
<%= 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>
the f.select part is what is incorrect but i think the IDEA of what I'm trying to do is captured here. I just don't know HOW to do it.
/update
I'm presenting a report in a web page and i need to allow the user to filter out which rows are available based on the title of a partner. The title of the partner however is not in the model that I've created the form for. The form is for the distribution model, which has a reference to matching_profile, which has a reference to partner. the partners table is the one that contains the title that I'd like to filter on.
Currently, I tried to accomplish this by adding another search_scope
search_scopes :equal => [:status, 'matching_profile.partner.title'] #obviously very wrong
update: I also tried this:
search_scopes :equal => [:status, 'distributions.matching_profile.partner.title']
this doesn't give me an error, but doesn't filter either. a step in the right direction? /update
but i'm getting an error because I can't seem to "hop" two tables to get to the title. I'm seeing this in the error message:
AND (matching_profile.partner.title = 'PARTNER_TITLE')
of course, this is wrong because there's no matching_profile field, only matching_profile_id.
I'm not sure what the best way to go about this is. Any advice is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我最终要做的是在我的可搜索特征中添加一个新选项以允许 :in 搜索。从那里我必须做一些重新调整以使序列化满意,但一旦我意识到我可以扩展可搜索特征,它就相对简单了。我添加了这个:
然后在我的分发模型中添加了:
最后在我看来我可以这样做:
并且控制器神奇地保持不变。
well, what I ended up having to do is adding a new option in my searchable trait to allow for an :in search. From there I had to do some re-jiggering to make the serialization happy, but it was relatively straightforward once i realized i could extend the searchable trait. I added this:
then in my distribution model I added:
and finally in my view I could do this:
and the controller magically stays the same.