I have a Commodity(name:string)
model and a Price(amount:float)
model such that:
class Commodity < ApplicationRecord
has_many :prices
end
And
class Price < ApplicationRecord
belongs_to :commodity
end
Thus, one commodity can have many prices.
I have a Ransack search form for Commodity, which obviously enables a user to search for commodities. I wish to include a sort_link(@q, :prices_amount)
sorting filter. Clicking this link should display all the commodities sorted according to their prices (if one commodity has multiple prices, that commodity will show multiple times, with each repeated commodity's price shown accordingly). Currently, Ransack does indeed show a commodity multiple times, (as many as the number of prices each commodity has) but I do not know a way to display that price for each repeated commodity.
I wish to know how should I display that price in my frontend. I display the query results via:
<%[email protected] do |commodity|%>
<%=commodity.name%>
<%=commodity.prices.first.amount%> <!-- In place of prices.first, I was to display the amount that Ransack found -->
<%end%>
I'm using prices.first.amount
right now as a placeholder. How do I replace this by the amount
that Ransack found while sorting?