0
<% @training_dates.reverse.each do |t| %>
  <% if t.candidate_limit != Subscription.group(:training_date_id).count[t.id] %>
      <%= t.name %>
  <% end %>
<% end %>

I want to turn this if statement into a scope so I can do @training_dates.(scope).reverse.each.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
cccvandermeer
  • 317
  • 2
  • 14
  • I think you need add some details about `@traqining_dates` variable, `Subscription` Model, and The scope add for Model not for Variable as: `Subscription.training_date_count` to return count of trainging_date – Mohamed Yakout Dec 30 '14 at 10:55
  • Why not use the scopes we helped you define in [Your Previous Question](http://stackoverflow.com/questions/27620689/named-scope-for-counting-children). This would be the equivalent. so `@training_dates = TrainingDate.not_full.reverse` then `@training_dates.each do |t|`. I am not sure what your comment on that post means when you say "doesn't work for new ones". It works based on when the call is made so obviously if someone signs up after the call there is not much you can do other than notify the user that the TrainingDate is now full. – engineersmnky Dec 30 '14 at 19:05

1 Answers1

0

I don't know what your other models are, but the sample scope for training_date model should like follows (for rails 4):

class TrainingDate
   scope :sss, -> do
      where.not(candidate_limit: Subscription.group(:training_date_id).count(:id))
           .order(:desc)
   end
end

and then:

sss.each { |t| t.name }

For other rails versions you have to user gem.

Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69