I have 2 tables, a PlayingField
table and Bookings
table, and I want to write a rails query that takes in query parameters Start_date
, End_date
and Number_of_players
我有2个表,一个PlayingField表和Bookings表,我想编写一个rails查询,它接受查询参数Start_date,End_date和Number_of_players
Scenario: I pass in Start_date, End_date and Number_of_players to a method and I want to Get all PlayingFields that are available (not booked) between the dates provided and that the field can accommodate the number of players wanted.
场景:我将Start_date,End_date和Number_of_players传递给一个方法,我希望获得所提供日期之间可用(未预订)的所有PlayFields,并且该字段可以容纳所需的玩家数量。
PlayingField has_many bookings
Booking belongs_to PlayingField
pseudo code:
伪代码:
@Fields = PlayingFields.where(bookings.end_date > start_date AND bookings.start_date > end_date AND PlayingFields.capacity >= number_of_players)
Can someone please tell me what the best way to do this is on the rails side of things?
有人可以请告诉我,最好的办法是在轨道方面吗?
How can I query the database through rails to get all available PlayingFields
between them dates and for the number of players required?
如何通过rails查询数据库以获取所有可用的PlayFields日期和所需的玩家数量?
Thanks
谢谢
1 个解决方案
#1
1
The rails would have to includ the bookings table before the SQL call.
在SQL调用之前,rails必须包含预订表。
PlayingFields.joins(:bookings).where(bookings.end_date > start_date AND bookings.start_date > end_date AND bookings.capacity >= number_of_players)
I'm also curious how those two objects programmatically relate. How does a Booking model become associated with a PlayingField ?
我也很好奇这两个对象是如何编程相关的。预订模型如何与PlayField相关联?
#1
1
The rails would have to includ the bookings table before the SQL call.
在SQL调用之前,rails必须包含预订表。
PlayingFields.joins(:bookings).where(bookings.end_date > start_date AND bookings.start_date > end_date AND bookings.capacity >= number_of_players)
I'm also curious how those two objects programmatically relate. How does a Booking model become associated with a PlayingField ?
我也很好奇这两个对象是如何编程相关的。预订模型如何与PlayField相关联?