Model's custom attributes not loaded if file is not in app/m


#1

This has puzzled me for at least a week. please help.

It seems that if a model class has any other custom attributes besides
DB
column attributes, and model class is not in /app/models/ folder, rails
does
not load custom attributes after 1st time, only DB column attributes.
only
way to see my custom attributes works is if model class has no module
and is
under /app/models/, not anywhere else.

--------------------- /app/models/user.rb , fullname() works

DB table users has columns: id, first, last.

class User < ActiveRecord::Base
def fullname
self.first + " " + self.last
end
end
------------------- /app/models/profile/poweruser.rb, ---------
module Profile
class Poweruser < ActiveRecord::Base
set_table_name “users”
def fullname
">>> power user: " + self.first + " " + self.last
end
end
end
--------------------- /app/controllers/userview_controller.rb, works

class UserviewController < ApplicationController
def index
@user = User.find(1)
logger.debug @user.fullname
end
end
----------------- /app/controllers/powerview_controller.rb, 1st time
works,
not after —
require ‘profile/poweruser’
class PowerviewController < ApplicationController
def index
@poweruser = Profile::Poweruser.find(1)
logger.debug @poweruser.fullname # 1st time works, not after
end
end

test steps:

  • 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
    works,
    refresh the page, works too.
  • Now stop and restart the script/server webrick, or lighttpd, first
    time
    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
redhat
(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
1.8.3)

only way to see my custom attributes works is if model class is not
under
module and in /app/models/, not anywhere else. This is a big constraint
for
me, since I’m creating quite a few separate functionalities, like
profile,
blog, store, etc. putting all model classes in one directory would drive
me
crazy. ideally, I like profile models (user, role, activity, etc) under
/app/models/profile/, blog models under /app/models/blog/.

Thanks.

Dorren