请帮帮忙!
如果我有:
class User < ActiveRecord::Base
has_many :user_shipments
has_many :shipments, through: :user_shipments
def posted_shipments
user_shipments.where(role: 'shipper').map(&:shipment)
end
def carrier_shipments
user_shipments.where(role: 'carrier').map(&:shipment)
end
end
和
class Shipment < ActiveRecord::Base
has_many :user_shipments
has_many :users, through: :user_shipments
def shippers
user_shipments.where(role: 'shipper').map(&:user)
end
def carriers
user_shipments.where(role: 'carrier').map(&:user)
end
end
和
class UserShipment < ActiveRecord::Base
belongs_to :user
belongs_to :shipment
end
我们如何将carrier
与shipper
关联起来,以便shipment
创建的每个shipper
,后者都可以查看他的carriers
进行反向拍卖我们正在尝试为项目构建的应用类型?
换句话说,User
shipper
会创建shipment
而User
carrier
可以对其进行出价,但仅针对属于他的shipper
,或shipment
创建时,shipper
可以立即查看他的carriers
列表并查看其出价?
非常感谢!我们是新手。
答案 0 :(得分:1)
要获得制作货件的个人用户的所有承运人列表:
@associated_carriers = user.posted_shipments.map{ |shipment| shipment.carriers}.uniq
要获取希望查看新货件的个人用户的所有发货人清单:
@associated_shippers = user.carrier_shipments.map{|shipment| shipment.shippers}.uniq
从这两个用户列表中,您可以遍历各个用户以获取有关每个用户的信息。
对于用户的@associated_shippers
,您可以通过以下方式列出发货人的邮件:
@associated_shippers.each do |shipper_user|
# in the auction view:
# shipper_user.posted_shipments, etc.
end
对于用户@associated_carriers
,您可以通过以下方式列出这些运营商carrier_shipments:
@associated_carriers.each do |carrier_user|
# in the auction view:
# carrier_user.carrier_shipments, etc.
end