Ruby on rails 3 代表不为我工作?
我已经看到代表团,我喜欢这个,我想定制如下 案例1:Ruby on rails 3 代表不为我工作?,ruby-on-rails-3,activerecord,Ruby On Rails 3,Activerecord,我已经看到代表团,我喜欢这个,我想定制如下 案例1: class Application < ActiveRecord::Base belongs_to :user delegate :name, :to => :user has_one :repair, :dependent => :destroy delegate :estimated_amount, :to => :repair has_one
class Application < ActiveRecord::Base
belongs_to :user
delegate :name, :to => :user
has_one :repair, :dependent => :destroy
delegate :estimated_amount, :to => :repair
has_one :dealership, :through => :booking
end
class User < ActiveRecord::Base
def name
return some value
end
end
类应用程序:用户
有一个:修复,:依赖=>:销毁
代表:估计金额:至=>:修理
有一个:经销商,:通过=>:预订
结束
类用户
我已经为#
案例2:我已经调用了Application.first.repair\u estimated\u amount:=>未定义的方法
案例3:我已经为#
有人能建议如何使用委托和一个关系吗
提前谢谢
Prasad您没有使用prefix选项,因此应该只调用不带前缀的方法
# model.rb
delegate :estimated_amount, :to => :repair
# somewhere else
Application.first.estimated_amount #=> works
Application.first.report_estimated_amount #=> exception!
但是,如果传递前缀选项:
# model.rb
delegate :estimated_amount, :to => :repair, :prefix => true
# somewhere else
Application.first.estimated_amount #=> exception
Application.first.report_estimated_amount #=> works!
有关详细信息,请参阅文档