Forum: Ruby on Rails Question about includes

Posted by William Herry (williamherry)
on 2013-02-01 10:07
(Received via mailing list)
I have three class,HotActivity, Activity和ActivityType

HotActivity and Activity is one to one association, Activity belongs to
ActivityType

in controller I code like this

@hot_activities = HotActivity.includes(:activity)

chrome will prompt me this

user: william
N+1 Query detected
  Activity => [:activity_type]
  Add to your finder: :include => [:activity_type]
N+1 Query method call stack

if I add like this

@hot_activities = HotActivity.includes(:activity => :activity_type)

it prompt this

user: william
Unused Eager Loading detected
  HotActivity => [:activity]
  Remove from your finder: :include => [:activity] activities:575
Uniform Notifier activities:587
user: william
Unused Eager Loading detected
  Activity => [:activity_type]
  Remove from your finder: :include => [:activity_type]

some one please help,

thanks in advs
Posted by unknown (Guest)
on 2013-02-03 00:06
(Received via mailing list)
HI william,

how exactly does chrome prompt that?

whats the view like?

is that the only line you have on your controller?

can you be a little more specific and post exactly what is the error you
get.

all the best,

Andre
Posted by William Herry (williamherry)
on 2013-02-04 05:43
(Received via mailing list)
hi andreo, thanks for reply

answer you inline


On Sun, Feb 3, 2013 at 7:04 AM, <andreo@benjamin.dk> wrote:

> HI william,
>
> how exactly does chrome prompt that?
>


> Uniform Notifier activities:551 <http://localhost:3000/activities>
> user: william N+1 Query detected Activity => [:activity_type] Add to your
> finder: :include => [:activity_type] N+1 Query method call stack
> /home/william/work/lly/app/views/activities/index.html.erb:48:in `block in
> _app_views_activities_index_html_erb___3943997569956187419_46069520'
> /home/william/work/lly/app/views/activities/index.html.erb:32:in
> `_app_views_activities_index_html_erb___3943997569956187419_46069520'
>

line 48

            <li><div id="type"></div>类型: <%=
hot_activity.activity.activity_type.name %></li>

line 32
            <li><div id="type"></div>类型: <%=
hot_activity.activity.activity_type.name %></li>

whats the view like?
>
       <% @hot_activities.each_with_index do |hot_activity, index| %>


        <div id="detail<%= index + 1 %>" class="detail cf">
          <ul>
            <li class="header">
              <%= link_to hot_activity.activity.title,
activity_path(hot_activity.activity) %>
            </li>
            <li>
              <div id="date"></div>
             ...

>
> is that the only line you have on your controller?
>

   def index
    @hot_activities = HotActivity.includes(:activity)
                                 .where("activities.status = 'open'")
                                 .order('start_at ASC')
                                 .limit(5)

    @closed_activities = Activity.includes(:participants)
                                 .where("status = 'closed'")
                                 .order("participants_count desc")
                                 .limit(5)

    @hot_sponsors = User.includes(:created_activities)
                        .where("status = 'A'")
                        .order("created_activities_count desc")
                        .limit(8)
  end
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.