User Model:
class User < ActiveRecord::Base
has_one :profile, :dependent => :destroy
delegate :gender, :to => :profile, :prefix=> false, :allow_nil => true
delegate :bio, :to => :profile, :prefix=> false, :allow_nil => true
... ...
end
问题来了,如果 profile 里有很多方法我想通过 delegate 这样加到 user 中来,要写很多 delegate...造轮子了
看到过有人写过这样的代码
class Post < ActiveRecord::Base
STATUSES = ['draft', 'published', 'spam']
validate_inclusion_of :status, :in => STATUSES
class <<self
STATUSES.each do |status_name|
define_method "all_#{status_name}" do
find(:all, :conditions => { :status => status_name }
end
end
end
end
想请教下,怎么通过这种方法解决写多个 delegate 的问题