由于方法调用的缓存导致的SearchLogic问题

时间:2010-11-03 01:30:25

标签: ruby-on-rails ruby searchlogic

我正在尝试使用searchlogic gem对几个表Post has_many assets执行搜索。如果不存在资产,我需要它来执行左外连接而不是内连接。

从下面的内容开始,查询是使用所需的外连接生成的,并传递前三个测试,但在最后一个测试中失败。但是,如果我只运行最后一次测试,那么它就会通过。

失败的原因是@search_logic_filter var仅在第一次测试时设置,并用于所有剩余的测试。

以这种方式设置@search_logic_filter的原因是它是对method_missing的唯一调用,它带有传递给Post.title_or_body_or...like("fun")的动态searchlogic方法调用的param

有没有更好的方法来设置过滤器参数?

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

**更新 这是为最终测试运行的查询。请注意,upload_file_name参数是'有趣',而不是'ruby_tips'。对于upload_file_name col的所有测试都存在'fun'参数,但它只对最后一次测试有用。

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)
)

1 个答案:

答案 0 :(得分:1)

您不应该以这种方式声明named_scope assets_upload_file_name_like。在第一次调用时,assets_upload_file_name_like命名范围定义为当时根据:conditions的值生成的@search_logic_filter的值。您应该在lambda上设置参数。

也无需使用method_missing。只需在named_scope课程中声明Post即可。作为奖励,应该过滤查询以防止SQL注入攻击。

class Post < ActiveRecord::Base
  named_scope :assets_upload_file_name_like, lambda { |file_name| {
    :joins => "left outer join assets on posts.id = assets.post_id",
    # Prevent SQL injection.
    :conditions => ["assets.upload_file_name like ?", "%#{file_name}%"]
  }}
end