hopefully there's a simple answer to this.
def index
@pagy, @drivers = pagy(
Driver.joins(:profile).select(
'drivers.*',
'(profiles.no_races + profiles.no_poles + profiles.no_podiums + profiles.no_wins) AS score'
).reorder('score DESC'),
page: params[:page],
items: 16
)
@drivers.each do |driver|
@subscription = driver.subscriptions.active.last
if @subscription
@sub_type = driver.subscription.stripe_plan
if @sub_type == "price_xxx" || @sub_type =="price_xxx"
@sub_type = 'gold'
elsif @sub_type == "price_xxx" || @sub_type == "price_xxx"
@sub_type = 'silver'
end
else
@sub_type = 'free'
end
end
end
I have a rails index page called drivers. In my controller, I select all drivers with a SQL query, which also sorts out the pagination with the @pagy gem and adds up their score.
After this I want to find out if they have a subscription and add a CSS class of gold, silver, or free depending on what sub they have in my template using @sub_type.
<div class="row mb-4">
<% @drivers.each do |driver| %>
<div class="col-sm-6 mb-3 col-md-4 mb-3 col-lg-3 mb-3 driverCard" itemscope itemtype="http://schema.org/Person">
<section>
<div class="card tier <%= @sub_type %>">
<div class="card-header">
<div class="flag rounded-circle">
.........
As a test, I have 32 drivers. One should be silver and one should be gold, and the rest should be free.
What actually is happening is the first page are all free, the second page are all gold and there's no silver on either page.
I'm guessing the subscriptions need to go in to the pagy query at the top of the controller? If that's the case, any pointers would be appreciated. Thanks