Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

NoMethodError on first page (Multiple associations)

#250

Hello, I have been trying to debug this issue and then i realized that plugin was causing this.

I am using the example of search from railscasts but I have a deep association model.

-----ERROR---------------------------------

NoMethodError in Browses#tracks

Showing app/views//tracks.html.erb where line #18 will_paginate ignores the :include with has_many raised:

You have a nil object when you didn't expect it! The error occurred while evaluating nil.setTitle Extracted source (around line #18 will_paginate ignores the :include with has_many):

15: 16: <% for track in @track do %> 17:

=> 18: <%= track.trackTitle%> - <%= track.artist.name %> (<%= track.dj_set.setTitle %>) 19:

20: <% end %> 21:

class Track belongs_to :dj_set belongs_to :artist end class dj_set has_many :tracks, :dependent => :destroy end class artist has_many :tracks end

------track.html.erb------ <% form_tag request.path, :method => 'get' do %> <% content_tag :label do %>

Search Tracks:
<%= text_field_tag :search, params[:search] %>

<% end %> <% end %>

<% for track in @track do %>
    <h3 style="margin-bottom: 2px">
        <%= track.trackTitle%> - <%= track.artist.name %> (<%= track.dj_set.setTitle %>)
    </h3>
<% end %>

------ Track.rb model -------

def self.search(search, page)

paginate :per_page => 20, :page => page,
         :include => [:artist, :dj_set],
         :conditions => ['trackTitle like ?', "%#{search}%"], :order => 'created_at'

end

----- browse_controller.rb ------

def tracks

@track = Track.search(params[:search], params[:page])

end

This WORKS fine on page >2 and if any search is performed. The eror is only shown on page one.

Correct output

Search Tracks: --intro--- Intro - A State of Trance (374) Intro - A State of Trance (375) Intro - A State of Trance (373) Intro - Trance Around the World (239) Intro - Trance Around the World (238)

Thanks you! Cheers, Akshay

Reported by akshay (at mugasha) · October 25th, 2008 @ 04:12 AM

State: invalid
Milestone: none
Assigned to: Mislav Mislav

Activity

  1. Mislav
    Mislav

    How are you sure the plugin is causing this? From what I see from your code and error, the problem isn't in pagination at all, but in the fact that one of the Tracks doesn't have dj_set.

    October 26th, 2008 @ 10:20 PM

  2. akshay (at mugasha)
    akshay (at mugasha)

    You are totally right. I just checked the table and one track had a dj_set NULL. This is my fault and I apologize for blaming the plugin. Thanks for pointing that out and all your work on this awesome plugin. Cheers, Akshay

    October 27th, 2008 @ 10:48 AM

  3. Mislav
    Mislav
    • State changed from new to invalid

    October 30th, 2008 @ 10:07 AM

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.