Rails,在 has_many 关联中查找具有多个日期的对象
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定我是否正确理解您的问题,但根据我的理解,您可以使用以下命令获得所需的结果:
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:
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.
现在我的脑海里浮现出另一个问题,但我不知道这是否可能。
如果您忘记了这种情况的从-到,我是否有可能检查此类关联,如下所示:
一栋房子附有 2 个日期。
用户选择 2 个日期。
现在我只想选择这两个日期都对应的房子..
像这样:
我尝试了上面的代码并得到了这样的 SQL:
但它似乎不起作用,这在单个查询中是否可能?单个查找()?
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:
I tried the code above and got an SQL like this:
But it doesn't seem to work, is this possible at all in a single query or single find( )?