NoMethodError on first page (Multiple associations)
Reported by akshay (at mugasha) | October 25th, 2008 @ 04:12 AM
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 raised:
You have a nil object when you didn't expect it! The error occurred while evaluating nil.setTitle Extracted source (around line #18):
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
Comments and changes to this ticket
-
Mislav October 26th, 2008 @ 10:20 PM
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.
-
akshay (at mugasha) October 27th, 2008 @ 10:48 AM
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
-
Mislav October 30th, 2008 @ 10:07 AM
- State changed from new to invalid
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.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป
Everyone's favorite Ruby library for pagination of practically anything!