Rails,在 has_many 关联中查找具有多个日期的对象

发布于 2024-10-11 06:39:52 字数 576 浏览 3 评论 0原文

我对 Rails 还很陌生,我遇到的问题描述如下:

首先,有一张有房子的桌子。 这些房屋有多个与 has_many :dates 关联的日期

其次是包含日期的表。 日期具有关联“belongs_to :house”

现在,在搜索中,用户可以选择开始日期和结束日期。 我想做的是搜索每栋房屋的日期,并检查这些日期是否在开始日期和结束日期之间。如果一个或多个日期在开始/结束日期内,则需要找到它。

这些是我搜索房屋的其他条件的条件:

parameters.push( [ params[:house_category_id], "houses.house_category_id = ?" ] );
parameters.push( [ params[:province_id], "houses.province_id = ?" ] );

我创建了一种方法来使用这些参数创建有效条件。

如果不够清楚,请告诉我,因为这有点难以解释。

我尝试过搜索这个,但它让我进入了除有答案的页面之外的所有其他页面。

谢谢

I'm pretty new to Rails, and the problem I'm bumping into is described as followes:

First, there is a table with houses.
The houses have multiple dates associated with has_many :dates

Second there is the table with dates.
Dates have the association belongs_to :house

Now, in a search, a user can select a start and end-date.
And what I want to do, is search each house for it's dates, and check if these are between the start and end-date. If one or more of the dates is within the start/end date, it needs to be found.

These are my conditions to search a house for other criteria:

parameters.push( [ params[:house_category_id], "houses.house_category_id = ?" ] );
parameters.push( [ params[:province_id], "houses.province_id = ?" ] );

I've created a method to create valid conditions with these parameters.

Please tell me, if it's not clear enough, cause it's a little hard to explain.

I've tried searching for this, but it got me on all kind of other pages except the pages with an answer..

Thanks

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

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

发布评论

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

评论(2

妞丶爷亲个 2024-10-18 06:39:52

不确定我是否正确理解您的问题,但根据我的理解,您可以使用以下命令获得所需的结果:

res = House.find(:all, :select => 'distinct houses.*', :joins => "INNER JOIN dates ON dates.house_id = houses.id", :conditions => "dates.date < '2011-01-12' AND dates.date > '2011-01-11'")

where '2011-01-12' & “2011-01-11”是用户选择的日期。

请告诉我这是否是您正在寻找的内容或发布更多详细信息。

Not sure if I understand your problem correctly, but according to what I understood, you can get the desired result using the following:

res = House.find(:all, :select => 'distinct houses.*', :joins => "INNER JOIN dates ON dates.house_id = houses.id", :conditions => "dates.date < '2011-01-12' AND dates.date > '2011-01-11'")

where '2011-01-12' & '2011-01-11' are the dates selected by the user.

Let me know if this is what you were looking for or post with more details.

娇妻 2024-10-18 06:39:52

现在我的脑海里浮现出另一个问题,但我不知道这是否可能。
如果您忘记了这种情况的从-到,我是否有可能检查此类关联,如下所示:

一栋房子附有 2 个日期。

用户选择 2 个日期。

现在我只想选择这两个日期都对应的房子..

像这样:

@houses = House.find(:all, :select => 'distinct houses.*', :joins => "INNER JOIN dates ON dates.house_id = houses.id", :conditions => "dates.date = '2011-01-12' AND dates.date = '2011-01-11'")

我尝试了上面的代码并得到了这样的 SQL:

SELECT distinct houses.* FROM `houses` INNER JOIN dates ON dates.house_id = houses.id WHERE (dates.date = '2011-01-15' AND dates.date = '2011-01-22')

但它似乎不起作用,这在单个查询中是否可能?单个查找()?

Now another question pops in my mind, but I don't know if that is possible..
If you forget about the from-to for this situation, is there any possibility that a I can check this sort of associations like so:

A house has 2 dates attached.

A User selects 2 dates.

Now I only want to select the house if these 2 dates both correspond..

Like this:

@houses = House.find(:all, :select => 'distinct houses.*', :joins => "INNER JOIN dates ON dates.house_id = houses.id", :conditions => "dates.date = '2011-01-12' AND dates.date = '2011-01-11'")

I tried the code above and got an SQL like this:

SELECT distinct houses.* FROM `houses` INNER JOIN dates ON dates.house_id = houses.id WHERE (dates.date = '2011-01-15' AND dates.date = '2011-01-22')

But it doesn't seem to work, is this possible at all in a single query or single find( )?

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