简体   繁体   English

Searchlogic:如何定义命名范围,以便可以在关联的模型上使用OR?

[英]Searchlogic: how do I define a namedscope so that I can use OR on an associated model?

So, I found this: Searchlogic OR condition results in undefined method 所以,我发现了这一点: Searchlogic OR条件导致未定义的方法

But that only works for fields on the model your are searching on directly. 但这仅适用于您直接搜索的模型上的字段。 I have a suscription model, but I want to search over the different subscriptions' account names OR their other_field, for example. 我有一个订阅模型,但是我想搜索不同订阅的帐户名或它们的other_field。

from the above link (and modified) I'd like to be able to do something similar to this: 通过以上链接(并进行了修改),我希望能够执行以下操作:

  named_scope :account_name_full_domain_like, lambda{ |name|{
     :conditions => ["accounts.name LIKE ? OR accounts.full_domain LIKE ?", "%" + name + "%", "%" + name + "%"],
     :joins => "LEFT JOIN `accounts` ON `accounts`.id = `subscriptions`.account_id"
    }}

but now I get an error on this: 但是现在我得到一个错误:

27:     <% form_for @search do |f| %>  

undefined method `subscription_subscription_path' for #<ActionView::Base:0x11061dbc0>

EDIT: figured it out: had to include .search in my call for assignment of the @search var 编辑:弄清楚了:在我要求@search var的调用中必须包含.search

@search = Subscription.search.account_name_full_domain_like(term)

Provided your named scope is on your Subscription model, you will be returning subscription records. 如果您的订阅模型上有命名范围,则将返回订阅记录。 Your condition and join seem to be valid. 您的条件和加入似乎有效。 I am not sure what the relationship of this named scope is to the instance variable @search you seem to be using in your view. 我不确定此命名范围与您在视图中似乎正在使用的实例变量@search之间的关系。

Is the path ' subscription_subscription_path ' where you were expecting to submit this form to? 您希望将此表单提交到的路径“ subscription_subscription_path ”吗? If so, run "rake routes", and ensure that you have a path named 'subscription_subscription_path'. 如果是这样,请运行“ rake route”,并确保您有一个名为“ subscription_subscription_path”的路径。

If you want to submit to an alternate path, you can specify the url to submit to in form_for by doing something like the following: 如果要提交到备用路径,则可以通过执行以下操作来在form_for中指定要提交的URL:

<% form_for @search, :url => subscription_path(@search) %>

More information on specifying a URL for form_for can be found here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for 有关为form_for指定URL的更多信息,可以在这里找到: http : //api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM