我正在尝试使用 searchlogic gem 对几个表执行搜索Post has_many assets
。如果资产不存在,我需要它来执行左外连接而不是内连接。
根据我在下面的内容,查询是使用所需的外部连接生成的,并通过了前三个测试,但最后一个测试失败。但是,如果我只运行最后一个测试,它就会通过。
失败的原因是@search_logic_filter
var 仅在第一个测试中设置,并用于所有剩余的测试。
之所以这样设置,@search_logic_filter
是因为只有method_missing调用携带了传递给动态searchlogic方法调用的paramPost.title_or_body_or...like("fun")
有没有更好的方法来设置过滤器参数?
test "find posts and assets by filter for user" do
customer = users(:customer)
create_post_for_user(customer, {:body => "Rails is fun", :tags => "rails ruby"})
create_post_for_user(customer, {:body => "Fun is what Emacs is all about", :title => "emacs"})
# File with post
asset_post = create_post_for_user(customer, {:body => "Ruby is pretty fun too",
:tags => "ruby"})
asset_post.assets << Asset.new(:upload_file_name => "ruby_tips",
:upload_file_size => 100,
:upload_content_type => "text")
asset_post.save
# search post
assert_equal 3, Post.find_for_user(customer.id, "fun").size
assert_equal 2, Post.find_for_user(customer.id, "ruby").size
assert_equal 1, Post.find_for_user(customer.id, "emacs").size
# search asset
puts "about to run last test"
assert_equal 1, Post.find_for_user(customer.id, "ruby_tips").size
end
class Post < ActiveRecord::Base
def self.find_for_user(user_id, filter, page=1)
Post.
user_id_equals(user_id).
title_or_body_or_tags_or_assets_upload_file_name_like(filter).all
end
class << self
def method_missing(name, *args, &block)
if name.to_s =~ /\w+_or_\w+_like$/
# ** only gets here once **
@search_logic_filter = args.first
super
elsif name == :assets_upload_file_name_like
# args is [] here which is the reason for the above setting of @search_logic_filter
named_scope :assets_upload_file_name_like, lambda {
{:joins => "left outer join assets on posts.id = assets.post_id",
:conditions => "assets.upload_file_name like '%#{@search_logic_filter}%'"}
}
assets_upload_file_name_like
else
super
end
end
end
end
** update 这是为最终测试运行的查询。请注意,upload_file_name 参数是“有趣”,而不是“ruby_tips”。upload_file_name col 的所有测试都存在“有趣”参数,但它只对最后一个测试很重要。
SELECT `posts`.*
FROM `posts`
left outer join assets
on posts.id = assets.post_id
WHERE (
((posts.title LIKE '%ruby_tips%') OR (posts.body LIKE '%ruby_tips%') OR (posts.tags LIKE '%ruby_tips%') OR (assets.upload_file_name like '%fun%'))
AND (posts.user_id = 20549131)
)