我希望能够动态创建从 ActiveRecord 继承的类,用于在 Rails 应用程序之外编写脚本。
我被困在这样的事情上:
require 'active_record'
def create_arec(table_name)
Class.new ActiveRecord::Base do
self.table_name = table_name
yield
end
end
Band = create_arec 'bands' do
scope :only_rock, -> {where genre: 'rock'}
end
rock_bands = Band.only_rock #undefined method `only_rock'
Run Code Online (Sandbox Code Playgroud)
我如何使它起作用,或者有人可以告诉我更好的方法吗?
我错过了什么吗?
class Circus
private
def start
puts 'And now for something completely different..'
end
end
Run Code Online (Sandbox Code Playgroud)
而我的幻灭:
c=Circus.new
c.start #NoMethodError: private method `start' called
c.method(:start).call #no problem at all
c.send :start #neither this fails
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我Ruby中存在私有方法的原因吗?