开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜