简体   繁体   中英

SearchLogic problems due to caching of method calls

I am trying to use the searchlogic gem to perform searches over a couple tables Post has_many assets . I need it to perform left outer joins rather than inner joins in the event of a non-existant asset.

From what I have below the query is generated with the required outer joins, and passes the first three tests, but fails on the last. However, if I only run the last test it then passes.

The reason for the failing is that the @search_logic_filter var is only being set on the first test and is used for all of the remaining tests.

The reason for the setting of the @search_logic_filter in this way is that it is the only call to method_missing that carries the param passed to the dynamic searchlogic method call of Post.title_or_body_or...like("fun")

Is there a better way to set the filter 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

** update This is the query that is run for the final test. Notice that the upload_file_name param is 'fun', not 'ruby_tips'. The 'fun' param exists for all the tests for the upload_file_name col, but it only matters for the last test.

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

You should not declare the named_scope assets_upload_file_name_like that way. When it's called the first time, the assets_upload_file_name_like named scope is defined with the the value for :conditions generated according the value of @search_logic_filter at that time. You should set the parameter on the lambda instead.

There's also no need to use method_missing . Just declare the named_scope within the Post class. As a bonus, the query should be filtered to guard against SQL injection attacks.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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