Activerecord Active Record Rails 3关联无法正常工作

Activerecord Active Record Rails 3关联无法正常工作,activerecord,ruby-on-rails-3,Activerecord,Ruby On Rails 3,我有以下与协会相关的课程: class Customer < ActiveRecord::Base has_many :orders has_many :tickets, :through => :orders has_many :technicians, :through => :ticket has_many :services, :through => :ticket end class Order < ActiveRecord::Base

我有以下与协会相关的课程:

class Customer < ActiveRecord::Base
  has_many :orders
  has_many :tickets, :through => :orders
  has_many :technicians, :through => :ticket
  has_many :services, :through => :ticket
end

class Order < ActiveRecord::Base
  belongs_to :customer
  has_many :tickets
  has_many :technicians, :through => :tickets
  has_many :services, :through => :tickets
end  

class Service < ActiveRecord::Base
  has_many :tickets
  has_many :technicians, :through => :tickets
  has_many :orders, :through => :tickets
end  

class Technician < ActiveRecord::Base
  has_many :tickets, :order => 'created_at DESC'
  has_many :services, :through => :tickets
  has_many :orders, :through => :tickets
end  

class Ticket < ActiveRecord::Base
  belongs_to :technician
  belongs_to :service
  belongs_to :order
end  
class客户:订单
有很多:技术人员,:通过=>:票
有很多:服务,:通过=>:票证
结束
类顺序:票
有很多:服务,:到=>:门票
结束
类服务:票
有很多:订单,:到=>:票
结束
类技师“已在DESC创建”
有很多:服务,:到=>:门票
有很多:订单,:到=>:票
结束
类票证
我能做到:
技术员、车票、服务、价格

但我做不到:
客户.订单.技术人员.名称
customer.orders.last.tickets.technology.name


如何从客户到技术人员或服务人员?

问题是您无法调用对象集合的属性

customer.orders.technician.name
这里有一组
订单
。每个
订单
可能有不同的
技师
。这就是为什么您不能对集合呼叫
技术人员

解决方案:对每个
订单
对象调用
技术人员

customer.orders.each do |order|
  order.technician.name
end
第二个例子也是如此。而不是:

customer.orders.last.tickets.technician.name
使用:

Groovy/Grails可以通过*-操作符来实现:-),该操作符在集合中的每个项上广播方法调用。超级漂亮,还有通过
的零授权,这是我在迁移到Rails时错过的第一件事。。。
customer.orders.last.tickets.each do |ticket|
  ticket.technician.name
end