【问题标题】:Joint query across 2 models (has_many)跨 2 个模型的联合查询 (has_many)
【发布时间】:2016-03-12 15:29:03
【问题描述】:

您好,我需要帮助,感谢所有见解。我有两个模型 Auctions 和 Bids,我想检索 current_user 赢得的所有拍卖,那些她/他已经出价高出的那些和他/她获胜的那些

这是两个模型:

class Auction < ActiveRecord::Base
  extend FriendlyId
  friendly_id :guid, use: :slugged
  before_save :populate_guid
  mount_uploaders :images, ImageUploader
  belongs_to :client
  has_many :bids, dependent: :destroy
  has_one  :order, dependent: :destroy
  validates_presence_of :title, :lien_price,
                    :end_time, :collateral_value,
                    :redemption_date, :current_interest_rate,
                    :additional_tax, :collateral_details,
                    :location, :client_id, :starting_bid
  validate :end_time_in_the_future, :on => :update
  validates_uniqueness_of :guid, case_sensitive: false  
  def end_time_in_the_future
    errors.add(:end_time, "can't be in the past") if self.end_time &&   self.end_time < Time.now
  end
  def self.get_active_auctions
    where("end_time > ?", Time.now)
  end
  def self.closed_auctions
    where("end_time < ?", Time.now)
  end
  def highest_bid
    self.bids.maximum("amount")
  end
  def highest_bid_object
    self.bids.order(:amount => :desc).limit(1).first
  end
  def highest_bidder
    self.highest_bid_object.user if highest_bid_object
  end
  def closed?
    self.end_time < Time.now
  end
  private 
  def populate_guid
    if new_record?
      while !valid? || self.guid.nil?
        self.guid = SecureRandom.random_number(1_000_000_000).to_s(36)
      end
    end
  end
end

class Bid < ActiveRecord::Base
  extend FriendlyId
  friendly_id :guid, use: :slugged
  belongs_to :auction
  belongs_to :user
  before_save :populate_guid
  validates_presence_of :amount, :user_id,
                    :auction_id

 #validate :higher_than_current?
 validates :amount, :numericality => true
 validates_uniqueness_of :guid, case_sensitive: false   
 def higher_than_current?
   if !Bid.where("amount > ? AND auction_id = ?", amount, self.auction.id).empty?
  errors.add(:amount, "is too low! It can't be lower than the current bid, sorry.")
  end  
end
private 
  def populate_guid
    if new_record?
      while !valid? || self.guid.nil?
        self.guid = SecureRandom.random_number(1_000_000_000).to_s(36)
      end
    end
  end  
end

我以为

@auctions = Auction.closed_auctions.where(highest_bidder: current_user)

@auctions = Auction.closed_auctions.joins(:bids).where(highest_bidder: current_user)

可以,但它们都会引发错误。

编辑此作品

@auctions = Auction.closed_auctions.references(highest_bidder: current_user)

但可能有更好的方法。

【问题讨论】:

    标签: ruby-on-rails postgresql activerecord subquery associations


    【解决方案1】:

    您可能无法从控制器访问 current_user(设计?)。因此,您需要将用户作为参数传递给类或实例方法。您应该研究的是范围,尤其是接受参数的范围。 Scopes 真的可以帮助你重构你的拍卖模型(你真的不需要任何只返回 where() 的方法),还可以解决无法访问的 current_user。

    在您的拍卖模型中像这样使用它:

    scope: :highest_bidder -> (current_user) { where(highest_bidder: current_user) }
    

    并从您的控制器中这样调用它:

    @auctions = Auction.closed_auctions.highest_bidder(current_user)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多