I have 2 models that I am currently testing with. One is the top level model (Account::User) and the other is an associated model (Account::Profile). I am having some difficulty pulling data from the associated model.
The code below is for Rails 3.1
I am getting the exception:
undefined method profile' for #<Class:0x7fe8aa0674b8>
Currently my models look like this:
Account::User:
class Account::User < ActiveRecord::Base
validates_presence_of :username, :first_name, :last_name, :instance_id, :user_type, :is_active
validates_uniqueness_of :username
has_one :profile, :class_name=> 'Account::Profile'
def self.all_by_user_type(user_type)
return all :conditions => ["user_type = ?", user_type]
end
def self.all_by_user_status(user_status)
return all :conditions => ["is_active = ?", user_status]
end
def self.all_by_last_login(last_login)
return all :conditions => ["last_login between ? and ?", last_login, Date.current]
end
def self.all_by_created_by(created_by)
return all :conditions => ["created_by = ?", created_by]
end
end
Account::Profile
class Account::Profile < ActiveRecord::Base
belongs_to :user, :class_name 'Account::User'
end
My controller action looks like this:
def dashboard
@user = Account::User.profile
end
When you write
Account::User.profile, you’re trying to access a class method namedprofile. Now,has_one(and friends) actually sets up the relationship on instances of the class, so you might use it like@user.profile.