This has puzzled me for at least a week. please help.
It seems that if a model class has any other custom attributes besides
column attributes, and model class is not in /app/models/ folder, rails
not load custom attributes after 1st time, only DB column attributes.
way to see my custom attributes works is if model class has no module
under /app/models/, not anywhere else.
--------------------- /app/models/user.rb , fullname() works
DB table users has columns: id, first, last.
class User < ActiveRecord::Base
self.first + " " + self.last
------------------- /app/models/profile/poweruser.rb, ---------
class Poweruser < ActiveRecord::Base
">>> power user: " + self.first + " " + self.last
--------------------- /app/controllers/userview_controller.rb, works
class UserviewController < ApplicationController
@user = User.find(1)
----------------- /app/controllers/powerview_controller.rb, 1st time
not after —
class PowerviewController < ApplicationController
@poweruser = Profile::Poweruser.find(1)
logger.debug @poweruser.fullname # 1st time works, not after
- create the model, controller, and rhtml pages.
- view the webpage, http://localhost:3000/userview, it always works.
- view the webpage, http://localhost:3000/powerview, first time it
refresh the page, works too.
- Now stop and restart the script/server webrick, or lighttpd, first
works, then refresh
the page, rails return “undefined method <custom_attribute>”.
*** note: unit and functional test passes though. Since first time view
works, but not subsequent view. I’ve tested on both windows XP and
(centos, RHEL clone), same behavior. I’ve tried cleared browser cache,
ruby-session cache, same.
environment: rails 1.0, mysql 5.0, winXP (ruby 1.8.2) and RHEL (ruby
only way to see my custom attributes works is if model class is not
module and in /app/models/, not anywhere else. This is a big constraint
me, since I’m creating quite a few separate functionalities, like
blog, store, etc. putting all model classes in one directory would drive
crazy. ideally, I like profile models (user, role, activity, etc) under
/app/models/profile/, blog models under /app/models/blog/.