下面代码find_by
部分会报SQLite3::SQLException: no such table
的错误
class SomeModel < ActiveRecord::Base
ALL_NAME = %w{ level_0 level_1 level_2 level_3}
enum name: ALL_NAME
class << self
def lol_scope
r = {}
ALL_NAME[0..3].map do |name|
selected = find_by(name: names[name]).first
r[name] = selected.blank? ? 0 : selected.rate
end
r
end
end
然后不管是bundle exec rake db:rest db:schema:load db:migrate
都会报错如下:
➜ lol git:(dota) ✗ be rake db:migrate --trace
/Users/lol/env/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.1.0/lib/active_support/values/time_zone.rb:285: warning: circular argument reference - now.
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: ......
但是如果写成scope
的方式:
scope :lol_scope, { find_by name: 2}
运行 rails s 是可以起来的,只是访问的时候会提示pre migrate
,然后在终端敲下 rake db:migrate
就 OK。
rails 4.1.0 ruby 2.2.2
,activerecord 上面那个rake db:migrate --trace
有写。
是在另外一个 model 中直接调用了这个 scope some_var = SomeModel.lol_scope
,如果当时把这个调用封装在一个方法里面就不会有这个 bug 了。
.....
private
def some_var
SomeModel.lol_scope
end