多个型号之间的导轨平均值

发布于 2024-08-04 04:21:07 字数 745 浏览 6 评论 0原文

我一直在尝试使用 Rails 关系做事,而不是拉出大型 SQL 连接,但我无法集中精力解决这个问题...

我有 3 个模型

酒店 客房 可用

它们都具有适当的 has_many 和 Belongs_to 关系。

我想要做的是在概述页面上列出特定城市的酒店,我想列出每个酒店找到的最低价格。

现在在 SQL 中我当然会在底部做一些代码,但是在 Rails 中我可以做这样的事情...

  def self.price
    Available.minimum(:price,
              :conditions => [ "price <> 0" ])
  end

这当然只是拉出所有这些的最低价格,而不是特定的 ID

问题是关系Hotel.find(1234).rooms.availables

但我想做这样的事情,可以进入我的循环而无需引用 ID?

SELECT MIN(availables.price)

FROM availables

INNER JOIN rooms ON rooms.id = availables.room_id
INNER JOIN hotels ON hotels.id = rooms.hotel_id

WHERE hotels.id = 5077 and rooms.private = 0 and availables.price <> 0

I've been trying to get my head around doing things using my Rails relationships as opposed to pulling out large SQL joins, but I can't wrap my head around this one...

I have 3 models

Hotels
Rooms
Availables

They all have the appropriate has_many and belongs_to relationships.

What I want to do is on the overview page listing the Hotels of a particular City, i want to list the lowest price found for each hotel.

Now in SQL I would of course do the bit of code at the bottom, but in rails I could do something like this...

  def self.price
    Available.minimum(:price,
              :conditions => [ "price <> 0" ])
  end

This of course just pulls the lowest price of all of them, not a specific ID

The problem is the relationship Hotel.find(1234).rooms.availables

But I'd like to do something like this that could go inside my loop without having to reference the ID?

SELECT MIN(availables.price)

FROM availables

INNER JOIN rooms ON rooms.id = availables.room_id
INNER JOIN hotels ON hotels.id = rooms.hotel_id

WHERE hotels.id = 5077 and rooms.private = 0 and availables.price <> 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

私藏温柔 2024-08-11 04:21:07

您可以通过在 Hotel 上设置 has_many :through 关系来实现此目的:

class Hotel < ActiveRecord::Base
  has_many :rooms
  has_many :availables, :through => :rooms

  # If you want "rooms"."private" condition, use these...
  has_many :public_rooms, :class_name => 'Room', :conditions => {:private => 0}
  has_many :public_availables, :through => :public_rooms, :source => :availables

  # This caches the value (potentially saving you db hits) for the
  # lifetime of the object, which you may or may not want depending
  # on your needs...
  def cheapest_available
    @cheapest_available ||= availables.minimum(:price, :conditions => ['availables.price > ?', 0])
  end
end

现在您可以循环遍历特定城市中显示最低价格的所有酒店:

@city.hotels.each do |hotel|
  puts "#{hotel.name}: #{hotel.cheapest_available}"
end

You can accomplish this by setting up a has_many :through relationship on Hotel:

class Hotel < ActiveRecord::Base
  has_many :rooms
  has_many :availables, :through => :rooms

  # If you want "rooms"."private" condition, use these...
  has_many :public_rooms, :class_name => 'Room', :conditions => {:private => 0}
  has_many :public_availables, :through => :public_rooms, :source => :availables

  # This caches the value (potentially saving you db hits) for the
  # lifetime of the object, which you may or may not want depending
  # on your needs...
  def cheapest_available
    @cheapest_available ||= availables.minimum(:price, :conditions => ['availables.price > ?', 0])
  end
end

Now you can loop through all of the hotels in a particular city displaying the lowest price:

@city.hotels.each do |hotel|
  puts "#{hotel.name}: #{hotel.cheapest_available}"
end
疏忽 2024-08-11 04:21:07

没关系!答案就在我面前,我只是没有得到正确的关联。

  def self.price
    Available.minimum(:price,
              :conditions => [ "price <> 0" ])
  end

使用后效果非常好,

:has_many, :through => :model

我只是没有意识到我必须设置更复杂的关系才能使其正常工作......

Nevermind! The answer was right in front of me, i just didn't get the proper association.

  def self.price
    Available.minimum(:price,
              :conditions => [ "price <> 0" ])
  end

This works perfectly after using

:has_many, :through => :model

I just didn't realize I had to setup a more complex relationship in order for it to work properly...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文