Can't run rake db:schema:load unless the database is already loaded
Tricky. My Rails models include lines like:
scope :unread, where(arel_table[:read].eq(false))
This line can't开发者_如何学C run, however, unless arel_table[:read]
is defined, and it's undefined unless the column exists. (This line could be rewritten not to use Arel, but some scopes cannot.)
However, when I try to run rake db:schema:load
, I get a NoMethodError
resulting from the fact that arel_table[:read]
is undefined. In short, it's a Catch-22. I can't load the database schema without running the environment, and I can't load the environment unless the database is already loaded.
Is there a better answer than "comment out all those lines then uncomment when done"? There's a number of offending lines.
That's the problem with using arel on scope. It may affect the migration. The simple solution is to just go raw SQL.
scope :unread, where('read = false')
The longer answer is that the class is somehow loaded when running the migration (it's normally is not loaded). If you can find what causes the loading of that class during the migration and work around it, then you can still use arel_table
in the scope. But probably it's not worth it.
精彩评论