我对RoR很陌生,在其他语言中我更喜欢的一种方法是定义fat模型的能力。看看其他的例子,这个想法似乎很直接,但是我在实现这个方法时遇到了一些问题。
我已经为测试创建了一个非常简单的模型:
class Account::User < ActiveRecord::Base
#validates_presense_of :username, :first_name, :last_name, :instance_id, :user_type, :is_active
#validates_uniqueness_of :username
def self.all_by_user_type(user_type)
p user_type
end
end当我尝试使用all_by_user_type方法时,我会得到下面的异常。在这方面的任何帮助都是非常感谢的。
/usr/lib/ruby/gems/1.8/gems/activerecord-3.1.3/lib/active_record/attribute_methods.rb:60:in
(Main):002:0> u.all_by_user_type(1) NoMethodError:未定义方法
all_by_user_type' for #<Account::User:0x7ffe60baebe8> from /usr/lib/ruby/gems/1.8/gems/activemodel-3.1.3/lib/active_model/attribute_methods.rb:385:inmethod_missing‘from :002:0> `method_missing’from (irb):2 from :0
发布于 2011-12-18 03:19:40
您正在定义一个类方法,使用类名(而不是实例)作为前缀来访问它。
https://stackoverflow.com/questions/8549575
复制相似问题