Reputation: 7011
This is horrible code, I know. How can I move it into the model when it's based on each record in a collection?
<% @brands.each do |b| %>
<% booleans = Preference.columns.select { |c| c.type == :boolean }.map(&:name) %>
<% trues = booleans.select { |name| b.preference.send(name) == true } %>
<%= trues.to_sentence.humanize %>
<% end %>
Upvotes: 0
Views: 120
Reputation: 26203
Place the logic into your model:
# app/models/brand.rb
def self.trues
self.all.each do |b| # OR whatever collection you're trying to iterate through
booleans = Preference.columns.select {|c| c.type == :boolean}.map(&:name)
trues = booleans.select {|name| b.preference.send(name) == true}
return trues
end
end
Then, display the returned value in your view:
# view
<%= Brand.trues.to_sentence.humanize %>
As a matter of convention, you may want to store the model convenience method to an instance variable in your controller, then render the instance variable from within your view:
# controller action
@trues = Brand.trues
# view
<%= trues.to_sentence.humanize %>
Upvotes: 1