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

发布于 2024-12-03 14:08:52 字数 880 浏览 3 评论 0原文

所以,我发现了这个:Searchlogic OR条件导致未定义的方法

但是仅适用于您直接搜索的模型上的字段。 我有一个订阅模型,但我想搜索不同订阅的帐户名称或其 other_field,例如。

从上面的链接(和修改)我希望能够做类似的事情:

  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"
    }}

但现在我得到一个错误:

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

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

编辑: 想通了: 必须在我的调用中包含 .search 来分配@search var

@search = Subscription.search.account_name_full_domain_like(term)

So, I found this: Searchlogic OR condition results in undefined method

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.

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 = Subscription.search.account_name_full_domain_like(term)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦过后 2024-12-10 14:08:52

如果您的命名范围位于您的订阅模型上,您将返回订阅记录。您的条件和加入似乎有效。我不确定这个命名范围与您在视图中似乎使用的实例变量 @search 之间的关系是什么。

您希望将此表单提交到的路径“subscription_subscription_path”是吗?如果是这样,请运行“rake paths”,并确保您有一个名为“subscription_subscription_path”的路径。

如果您想提交到备用路径,您可以通过执行以下操作来指定要在 form_for 中提交的 url:有关

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

为 form_for 指定 URL 的更多信息可以在此处找到:http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for

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.

Is the path 'subscription_subscription_path' where you were expecting to submit this form to? If so, run "rake routes", and ensure that you have a path named '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 @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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文