问题描述
我有以下类与关联:
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
我可以这样做:
technician.tickets.service.price
I can do:
technician.tickets.service.price
但我不能这样做:
customer.orders.technician.name
customer.orders.last.tickets.technician.name
But I can't do:
customer.orders.technician.name
customer.orders.last.tickets.technician.name
我该如何去从客户的技术人员或服务?
How do I go from customer to technician or service?
推荐答案
现在的问题是,你不能叫一个属性上对象的集合。
The problem is that you cannot call a property on a collection of objects.
customer.orders.technician.name
在这里,您有订单的集合
。每个序
可以有不同的技术人员
。这就是为什么你不能叫技术人员
上的集合。
Here you have a collection of orders
. Each order
could have a different technician
. That's why you cannot call technician
on a collection.
解决方法:拨打技术人员
每个顺序
目标:
Solution: call technician
on each order
object:
customer.orders.each do |order|
order.technician.name
end
也是一样的你的第二个例子。相反的:
Same goes for your second example. Instead of:
customer.orders.last.tickets.technician.name
使用:
customer.orders.last.tickets.each do |ticket|
ticket.technician.name
end
这篇关于活动记录的Rails 3联工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!