Text

Nov 9, 2010
@ 10:59 pm
Permalink
1 note

Database views and models in Rails 3

Rails models can have a database view instead of a table, if you so desire. Just follow the pluralize convention:

What won’t work out of the box is preloading associations, since views (at least in Postgres) don’t have primary keys. This means Rails can’t tell rows apart when calling #uniq on an array of results.

Solution: define #hash and #eql? (actually, either one seems to work, but it’s recommended to define both per http://ruby-doc.org/core/classes/Hash.html).

And that’s that. Search.inspect returns “Search(Table doesn’t exist)”, but that’s a minor detail.

  1. ecin posted this