I have the following method…
def index
@posts=Post.find(:all, :include => [:user, :comments])
for post in @posts
puts “Post: #{post.title}”
puts “Written by: #{post.user.title}”
puts “Last comment on: #{post.comments.first.created_at}”
end
end
(the use model)
class User < ActiveRecord::Base
has_many:posts
end
(the post model)
class Post < ActiveRecord::Base
belongs_to:user
has_many :comments, :order => ‘created_at DESC’
end
when i run the application I get the following errors why ? Am i missing
something ???
NoMethodError in TestController#index
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.title
RAILS_ROOT: D:/InstantRails-2.0-win/rails_apps/active
Application Trace | Framework Trace | Full Trace
app/controllers/test_controller.rb:6:in index' app/controllers/test_controller.rb:4:in
each’
app/controllers/test_controller.rb:4:in `index’