dougal / acts_as_indexed

Acts As Indexed is a plugin which provides a pain-free way to add fulltext search to your Ruby on Rails app
http://douglasfshearer.com/blog/rails-plugin-acts_as_indexed
MIT License
211 stars 49 forks source link

ActiveRecord macro does not respect alias_attribute #4

Closed JonnieCache closed 14 years ago

JonnieCache commented 14 years ago

Hi

I have a model, with a string field, name, and I am using the alias_attribute (http://bit.ly/aZ25eE) method to alias this field to title. I then define indexing on this aliased field with acts_as_indexed :fields => [:title]. Fulltext queries on this model should then return results from the name column, however they don't.

I know this is a minor thing, but I sometimes lose track of which is the original column and which is the alias, which is kind of the point. I don't want to have to care about which is the original and which is the alias, I want to be able to use whichever is most human-readable and makes most sense at the time.

dougal commented 14 years ago

Can you provide a simplified example of the model as you have described?

Thanks.

JonnieCache commented 14 years ago
# assume a text column called name actually exists
class Test < ActiveRecord::Base
  alias_attribute :title, :name
  acts_as_indexed :fields => [:title]
end

t = Test.create :name => 'one two three'
# the following is true
t.title == t.name
# however this is false
t.with_query('one three').present?

I realise that this is relatively trivial but I am trying to get into the habit of submitting issues when I see things I think could be improved in a gem, rather than just forgetting about it :)

dougal commented 14 years ago

OK, I had a go at replicating this but came up blank. When AAI queries for an attribute's contents, it simply calls send(:field_name) on the object to be saved to the index, so it should work fine with alias'. I have included my test code below, although I have the name and title fields the other way round.

class CreateFoobars < ActiveRecord::Migration
  def self.up
    create_table :foobars do |t|
      t.string :title
      t.timestamps
    end
  end

  def self.down
    drop_table :foobars
  end
end

class Foobar < ActiveRecord::Base
  acts_as_indexed :fields => [:name]

  alias_attribute :name, :title
end

require 'test_helper'

class FoobarTest < ActiveSupport::TestCase

  setup do
    @foobar = Foobar.create(:title => 'badgers eat toast')
  end

  def test_should_be_a_valid_record
    assert !@foobar.new_record?
  end

  def test_alias_foo_to_title
    assert_equal @foobar.title, @foobar.name
  end

  def test_should_return_record
    assert_equal [@foobar], Foobar.with_query('toast').all
  end

end

I'm closing this for now, but if you have anything further on this, please don't hesitate to reopen it.

Great policy on reporting bugs in libraries, I do the same, and very much appreciate it when people take the time to report those on my projects.

Thanks.