one to many relationship and includes
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
开发者_JAVA技巧
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 %>
<% @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.
精彩评论