用于反向拍卖类型的应用程序的Ruby on Rails

时间:2015-08-19 02:47:27

标签: ruby-on-rails ruby model-associations

请帮帮忙!

如果我有:

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

我们如何将carriershipper关联起来,以便shipment创建的每个shipper,后者都可以查看他的carriers进行反向拍卖我们正在尝试为项目构建的应用类型?

换句话说,User shipper会创建shipmentUser carrier可以对其进行出价,但仅针对属于他的shipper,或shipment创建时,shipper可以立即查看他的carriers列表并查看其出价?

非常感谢!我们是新手。

1 个答案:

答案 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