activerecord-hackery / meta_search

Object-based searching (and more) for simply creating search forms. Not currently maintained.
http://erniemiller.org/2013/11/17/anyone-interested-in-activerecord-hackery/
MIT License
903 stars 140 forks source link

Database views not supported? #32

Closed cjamison closed 13 years ago

cjamison commented 13 years ago

I have a rails 2 app that uses searchlogic that I am converting to rails 3 and trying to use meta-search. Everything seems to work just fine except when I try to search models that are based upon a database view. I get a variant of the following error when clicking the submit button on the search form:

NoMethodError in ReportsController#action
undefined method `eq' for nil:NilClass

activesupport (3.0.7) lib/active_support/whiny_nil.rb:48:in `method_missing'
meta_search (1.0.5) lib/meta_search/where.rb:106:in `send'
meta_search (1.0.5) lib/meta_search/where.rb:106:in `evaluate'
meta_search (1.0.5) lib/meta_search/where.rb:106:in `map'
meta_search (1.0.5) lib/meta_search/where.rb:106:in `evaluate'
meta_search (1.0.5) lib/meta_search/builder.rb:217:in `set_attribute_method_value'
meta_search (1.0.5) lib/meta_search/builder.rb:138:in `method_missing'
meta_search (1.0.5) lib/meta_search/builder.rb:118:in `send'
meta_search (1.0.5) lib/meta_search/builder.rb:118:in `assign_attributes'
meta_search (1.0.5) lib/meta_search/builder.rb:117:in `each_pair'
meta_search (1.0.5) lib/meta_search/builder.rb:117:in `assign_attributes'
meta_search (1.0.5) lib/meta_search/builder.rb:92:in `build'
meta_search (1.0.5) lib/meta_search/searches/active_record.rb:43:in `search'
activerecord (3.0.7) lib/active_record/associations/association_collection.rb:444:in `send'
activerecord (3.0.7) lib/active_record/associations/association_collection.rb:444:in `method_missing'
activerecord (3.0.7) lib/active_record/base.rb:1122:in `with_scope'
activerecord (3.0.7) lib/active_record/associations/association_proxy.rb:207:in `send'
activerecord (3.0.7) lib/active_record/associations/association_proxy.rb:207:in `with_scope'
activerecord (3.0.7) lib/active_record/associations/association_collection.rb:440:in `method_missing'

The undefined method could be "matches" or "gteq" or whatever condition I am using for a field on the form. I see this on rails 3.0.7, postgresql 8.4, and pg gem 0.11.0.

Is there a simple resolution to this or is it a bit more complicated? I know there are differences in even Rails 2 to Rails 3. For example, using my database view based model in Rails 2 the command "Model.find(1)" works whereas in Rails 3 the same command results in

NoMethodError: undefined method `eq' for nil:NilClass
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-3.0.7/lib/active_support/whiny_nil.rb:48:in `method_missing'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.7/lib/active_record/relation/finder_methods.rb:299:in `find_one'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.7/lib/active_record/relation/finder_methods.rb:289:in `find_with_ids'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.7/lib/active_record/relation/finder_methods.rb:107:in `find'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.7/lib/active_record/base.rb:439:in `__send__'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.7/lib/active_record/base.rb:439:in `find'
from (irb):3

Interestingly, "Model.find_by_id(1)" works in both Rails 2 and Rails 3.

I would greatly appreciate any guidance in this area.

ernie commented 13 years ago

In your app, what happens if you do Model.arel_table[:attribute_name] ?

cjamison commented 13 years ago

I get nil for every attribute I try. In addition, just running Model.arel_table gives the following output:

=> #<Arel::Table:0xaa502e4 @name="model", @primary_key=nil, @table_exists=false, @table_alias=nil, @aliases=[], @columns=nil, @engine=ActiveRecord::Base>

Many thanks, Creston

ernie commented 13 years ago

That's what I expected... MetaSearch builds its conditions off of ARel attributes, and if a view doesn't provide access to its columns as an attribute, then I'm not sure there's a good solution on my end. :/