Searchlogic:如何定义命名范围以便我可以在关联模型上使用 OR ?
所以,我发现了这个: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的命名范围位于您的订阅模型上,您将返回订阅记录。您的条件和加入似乎有效。我不确定这个命名范围与您在视图中似乎使用的实例变量 @search 之间的关系是什么。
您希望将此表单提交到的路径“subscription_subscription_path”是吗?如果是这样,请运行“rake paths”,并确保您有一个名为“subscription_subscription_path”的路径。
如果您想提交到备用路径,您可以通过执行以下操作来指定要在 form_for 中提交的 url:有关
为 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:
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