Undefined method `fullname' for #<User:0x357e380>, BUT works

Hi all,

Please excuse the long post, but I wanted to make sure you have all the
information…

I have a NewsModel that looks like this:

class News < ActiveRecord::Base

belongs_to :user

validates_associated :user
validates_length_of :title, :description, :minimum => 5, :allow_nil
=> false

acts_as_commentable
acts_as_taggable

def self.find_recent
News.find(:all, :order => ‘created_at’, :limit => 10).reverse
end

end

and a controller like this:
class NewsController < ApplicationController

def show
begin
@news = News.find(params[:id])
rescue ActiveRecord::RecordNotFound
flash[:notice] = “News item not found!”
redirect_to :action => ‘index’ and return
else
@comments = @news.comments
end
end
end

and finally the view:
–Show

<%= render :partial => '/news/item', :locals => { :item => @news } %>
    <%= render :partial => '/comments/comment', :collection =>

@comments
%>

    <%= render :partial => '/comments/form' %>

–/comments/comment

Comments

<%= comment.title -%>
<%= h comment.comment -%>
<%= comment.user.fullname -%> <%= time_ago_in_words(comment.created_at) -%> ago

As you can see, the comments partial calls comment.user.fullname, which

is defined in the UserModel:
class User < ActiveRecord::Base

has_many :documents
has_many :events
has_many :news
has_many :comments

def fullname
“#{firstname} #{surname}”
end

end

When the server is first started the page renders correctly, HOWEVER if

I hit refresh it fails with:
undefined method `fullname’ for #User:0x357e380

Extracted source (around line #10):

7: <%= h comment.comment -%>
8:
9:


10: <%= comment.user.fullname -%> <%=
time_ago_in_words(comment.created_at) -%> ago
11:

12:

13:

Restarting the server fixes the issue for 1 view, then all subsequent
views fail. Here is the console log:

Andrews-MacBook:~/Sites/tassia andrewskegg$ ./script/server
=> Booting Mongrel (use ‘script/server webrick’ to force WEBrick)
=> Rails application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
** Starting Mongrel listening at 0.0.0.0:3000
** Starting Rails with development environment…
** Rails loaded.
** Loading any Rails specific GemPlugins
** Signals ready. TERM => stop. USR2 => restart. INT => stop (no
restart).
** Rails signals registered. HUP => reload (without restart). It
might not work well.
** Mongrel available at 0.0.0.0:3000
** Use CTRL-C to stop.

Processing NewsController#show (for 127.0.0.1 at 2006-12-20 10:49:28)
[GET]
Session ID: ea9a336b5b4d1bee4d647d25de2a27e7
Parameters: {“action”=>“show”, “id”=>“13”, “controller”=>“news”}
News Columns (0.000266) SHOW FIELDS FROM news
News Load (0.000302) SELECT * FROM news WHERE (news.id = 13)
Rendering within layouts/application
Rendering news/show
Tagging Columns (0.000286) SHOW FIELDS FROM taggings
Tag Load (0.000444) SELECT tags.* FROM tags INNER JOIN taggings ON
tags.id = taggings.tag_id WHERE ((taggings.taggable_type = ‘News’) AND
(taggings.taggable_id = 13))
Tag Columns (0.000187) SHOW FIELDS FROM tags
Rendered /news/_item (0.00561)
Comment Load (0.000301) SELECT * FROM comments WHERE
(comments.commentable_id = 13 AND comments.commentable_type = ‘News’)
Comment Columns (0.000248) SHOW FIELDS FROM comments
User Columns (0.000345) SHOW FIELDS FROM users
User Load (0.000359) SELECT * FROM users WHERE (users.id = 1)
Rendered /comments/_comment (0.01381)
Rendered /comments/_form (0.00201)
Completed in 0.04776 (20 reqs/sec) | Rendering: 0.02942 (61%) | DB:
0.00274 (5%) | 200 OK [http://localhost/news/show/13]

Processing NewsController#show (for 127.0.0.1 at 2006-12-20 10:49:32)
[GET]
Session ID: ea9a336b5b4d1bee4d647d25de2a27e7
Parameters: {“action”=>“show”, “id”=>“13”, “controller”=>“news”}
News Columns (0.000273) SHOW FIELDS FROM news
News Load (0.000314) SELECT * FROM news WHERE (news.id = 13)
Rendering within layouts/application
Rendering news/show
Tag Load (0.000489) SELECT tags.* FROM tags INNER JOIN taggings ON
tags.id = taggings.tag_id WHERE ((taggings.taggable_type = ‘News’) AND
(taggings.taggable_id = 13))
Rendered /news/_item (0.00340)
Comment Load (0.000447) SELECT * FROM comments WHERE
(comments.commentable_id = 13 AND comments.commentable_type = ‘News’)
User Columns (0.000271) SHOW FIELDS FROM users
User Load (0.000356) SELECT * FROM users WHERE (users.id = 1)

ActionView::TemplateError (undefined method `fullname’ for
#User:0x357e380) on line #10 of app/views/comments/_comment.rhtml:
7: <%= h comment.comment -%>
8:
9:


10: <%= comment.user.fullname -%> <%=
time_ago_in_words(comment.created_at) -%> ago
11:

12:

13:

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_recor
d/base.rb:1848:in
`method_missing’

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_recor
d/associations/association_proxy.rb:123:in
`send’

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_recor
d/associations/association_proxy.rb:123:in
method_missing' #{RAILS_ROOT}/app/views//comments/_comment.rhtml:10:in _run_rhtml_47app47views47comments47_comment46rhtml’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_view/ba
se.rb:326:in
`send’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_view/ba
se.rb:326:in
`compile_and_render_template’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_view/ba
se.rb:301:in
`render_template’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_view/ba
se.rb:260:in
`render_file’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_view/ba
se.rb:275:in
`render’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_view/pa
rtials.rb:59:in
`render_partial’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/benchmarking.rb:26:in
benchmark' /usr/local/lib/ruby/1.8/benchmark.rb:293:in measure’
/usr/local/lib/ruby/1.8/benchmark.rb:307:in `realtime’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/benchmarking.rb:26:in
`benchmark’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_view/pa
rtials.rb:58:in
`render_partial’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_view/pa
rtials.rb:73:in
`render_partial_collection’

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_recor
d/base.rb:1361:in
`each_with_index’

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_recor
d/associations/association_proxy.rb:123:in
`each’

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_recor
d/associations/association_proxy.rb:123:in
`each_with_index’

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_recor
d/associations/association_proxy.rb:123:in
`send’

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_recor
d/associations/association_proxy.rb:123:in
`method_missing’

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_recor
d/associations/has_many_association.rb:98:in
`method_missing’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_view/pa
rtials.rb:71:in
`render_partial_collection’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_view/ba
se.rb:285:in
render' #{RAILS_ROOT}/app/views/news/show.rhtml:4:in _run_rhtml_47app47views47news47show46rhtml’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_view/ba
se.rb:326:in
`send’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_view/ba
se.rb:326:in
`compile_and_render_template’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_view/ba
se.rb:301:in
`render_template’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_view/ba
se.rb:260:in
`render_file’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/base.rb:786:in
`render_file’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/base.rb:694:in
`render_with_no_layout’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/layout.rb:247:in
`render_without_benchmark’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/benchmarking.rb:50:in
render' /usr/local/lib/ruby/1.8/benchmark.rb:293:in measure’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/benchmarking.rb:50:in
`render’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/base.rb:1069:in
`perform_action_without_filters’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/filters.rb:635:in
`call_filter’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/filters.rb:622:in
`perform_action_without_benchmark’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/benchmarking.rb:66:in
perform_action_without_rescue' /usr/local/lib/ruby/1.8/benchmark.rb:293:in measure’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/benchmarking.rb:66:in
`perform_action_without_rescue’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/rescue.rb:81:in
`perform_action’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/base.rb:427:in
`send’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/base.rb:427:in
`process_without_filters’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/filters.rb:627:in
`process_without_session_management_support’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/session_management.rb:114:in
`process’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/base.rb:330:in
`process’

/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6.5618/lib/dispatcher.rb:41:in

`dispatch’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel/rails.rb:84:
in
process' /usr/local/lib/ruby/1.8/sync.rb:229:in synchronize’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel/rails.rb:83:
in
`process’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel.rb:580:in

`process_client’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel.rb:579:in

`each’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel.rb:579:in

`process_client’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel.rb:686:in

`run’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel.rb:686:in

`initialize’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel.rb:686:in

`new’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel.rb:686:in

`run’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel.rb:673:in

`initialize’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel.rb:673:in

`new’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel.rb:673:in

`run’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel/configurator
.rb:267:in
`run’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel/configurator
.rb:266:in
`each’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel/configurator
.rb:266:in
`run’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/bin/mongrel_rails:127:in

`run’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongrel/command.rb:2
11:in
`run’

/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/bin/mongrel_rails:231

/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/lib/active_suppo
rt/dependencies.rb:482:in
`load’

/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/lib/active_suppo
rt/dependencies.rb:482:in
`load’

/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/lib/active_suppo
rt/dependencies.rb:337:in
`new_constants_in’

/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/lib/active_suppo
rt/dependencies.rb:482:in
`load’

/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6.5618/lib/commands/servers/mon
grel.rb:52
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require' /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in require’

/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/lib/active_suppo
rt/dependencies.rb:489:in
`require’

/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/lib/active_suppo
rt/dependencies.rb:337:in
`new_constants_in’

/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/lib/active_suppo
rt/dependencies.rb:489:in
`require’

/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6.5618/lib/commands/server.rb:3
9
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require' /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in require’

/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/lib/active_suppo
rt/dependencies.rb:489:in
`require’

/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/lib/active_suppo
rt/dependencies.rb:337:in
`new_constants_in’

/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/lib/active_suppo
rt/dependencies.rb:489:in
`require’
script/server:3

Rendering
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib/action_control
ler/templates/rescues/layout.rhtml
(500 Internal Error)

Any ideas?

Your user model may be getting unloaded. Are you sure your User class
is defined in app/models/user.rb?

If the filename is different rails may to reload it properly in
development mode.

Thanks for the reply.

I have had a play around with the filenames and thought I was onto
something, but the computer says no.
The file is app/models/user.rb and contains:
class User < ActiveRecord::Base

has_many :documents
has_many :events
has_many :news
has_many :comments

def fullname
“#{firstname} #{surname}”
end

end

I am using Rails 1.2 RC1 (ie 1.1.6) in development mode on my mac.

Interestingly, I am getting what looks like the results of “rake
db:test” whenever I run a rake command, including “./scripts/about”. I
wonder if this is related?

Alex W. wrote:

Your user model may be getting unloaded. Are you sure your User class
is defined in app/models/user.rb?

If the filename is different rails may to reload it properly in
development mode.

Hi Everyone,

Whatever the problem was, it has been fixed since RC1.

I just upgraded to edge rails and everything works perfectly.

Thanks for the reply.

I have had a play around with the filenames and thought I was onto
something, but the computer says no.
The file is app/models/user.rb and contains:
class User < ActiveRecord::Base

has_many :documents
has_many :events
has_many :news
has_many :comments

def fullname
“#{firstname} #{surname}”
end

end

I am using Rails 1.2 RC1 (ie 1.1.6) in development mode on my mac.

Interestingly, I am getting what looks like the results of “rake
db:test” whenever I run a rake command, including “./scripts/about”. I
wonder if this is related?

On Dec 20, 12:25 pm, Alex W. [email protected]