model.where(:name => "string) INTERNAL ERROR!!! No attribute exists!
December 17, 2012 7:48 AM   Subscribe

In Rails, I'm using a model to represent a database view. I'm getting an error when I try to search on an attribute. (gist).

Not sure why this is happening, especially the fact that model.where(:attribute => "string") has an error but model.where("attribute = ?", "string") does not. Anyone run into this before? I haven't used a model to represent a view before, only to represent a table.
posted by czytm to Computers & Internet (8 answers total)
 
It looks like ActiveRecord can't read column information from the DB/view. What database are you using? What happens if you call
Morphology::MatrixView.reset_column_information ?
posted by bbuda at 8:04 AM on December 17, 2012


Response by poster: Using postgres.

(rdb:24) Morphology::MatrixView.reset_column_information
nil

posted by czytm at 10:09 AM on December 17, 2012


Try these two commands and compare the generated SQL. It might point you in the right direction. I'm assuming you're using Rails 3+

Morphology::MatrixView.where(:name => 'anex').to_sql

Morphology::MatrixView.where("name = ?", "anex").to_sql
posted by ndfine at 10:15 AM on December 17, 2012


Response by poster: Heres the output problem from the logger https://gist.github.com/4320589. Looks like it might have something to do with MetaWhere gem. I've tried googling (and stackoverflow) for other people with problems with sql views and metawhere but haven't been able to find anything meaningful.

(ndfine, the error on the to_sql method is the same as the error linked in this gist)
posted by czytm at 10:31 AM on December 17, 2012


Response by poster: debugger inside metawhere (def attribute_from_column_and_table(column,table))

/home/cl/.rvm/gems/ruby-1.9.2-p180/gems/meta_where-1.0.4/lib/meta_where/utility.rb:15
case column
(rdb:2) column
:name
(rdb:2) table
Arel::Table:0xc45b300 @name="morphology_matrix_view", @engine=ActiveRecord::Base, @columns=nil, @aliases=[], @table_alias=nil, @primary_key=nil
(rdb:2) table[column]
nil


posted by czytm at 10:41 AM on December 17, 2012


I don't think that views appear in pg_tables, do they? Views are in pg_views.
posted by sbutler at 10:42 AM on December 17, 2012


Response by poster: You're correct, sbutler.
posted by czytm at 10:49 AM on December 17, 2012


Response by poster: Ok I found a hack to fix it. http://stackoverflow.com/questions/5494779/connecting-a-rails-model-to-a-database-view

Thanks for everyone's help. We've been using this view for a while but only recently implemented searching which showed the error.

I swear I spent at least 30-45 minutes googling and searching stackoverflow
posted by czytm at 10:56 AM on December 17, 2012 [1 favorite]


« Older My boring ass internet connectivity problem   |   All in the desert fighting a war against mutants... Newer »
This thread is closed to new comments.