Convert Sql Statement To Arel Rails Query
So, let's get straight: I'm struggling to days do convert this SQL Statement: select * from ( select distinct on (program_id) editions.*, programs.* from editions inner jo
Solution 1:
You can use the from method to accomplish the tricky part of your query (selecting from a subquery). The subquery itself should be a straightforward mapping (select, joins, where, etc). So the result would be something like this:
Edition.select('*')
.from(
Editions.select('DISTINCT ON (program_id) editions.*, programs.*')
.joins(:programs)
.where(programs: { station_id: 1 })
)
.order(fixed: :desc, published_at: :desc, time: :desc)
.limit(4)
This will return Edition records, with additional data from the Program record stored on each. To avoid the weirdness of that result type, which may also just throw errors and be unable to resolve, you could either use Arel directly (similar syntax) or use .pluck on the end to pluck only the columns you actually need.
Solution 2:
Recently I found this cool project
http://www.scuttle.io/
Post a Comment for "Convert Sql Statement To Arel Rails Query"