Reputation: 133
My problem is: each entry in view calls new sql query altough i used includes in controller. These sql queries are not efficient. Any help will be appreciated.
Entry model
has_many :training_entries
TrainingEntry model
belongs_to :entry
entries controller
@entries = Entry.includes(:training_entries)
view
<% @entries.each do |entry| %>
<% if entry.training_entries.where("category_id =?",1).exists? %>
ok
<% end %>
<% end %>
Upvotes: 2
Views: 72
Reputation: 640
<% @entries.each do |entry| %>
<% if entry.training_entries.detect { |t_entr| t_entr.category_id == 1 } %>
ok
<% end %>
<% end %>
change detect with select if you need to filter more than one element.
Upvotes: 2