我有2個模型,我目前正在測試。一個是頂級模型(Account :: User),另一個是關聯模型(Account :: Profile)。我在從相關模型中提取數據時遇到了一些困難。從關聯模型獲取數據
下面的代碼是爲Rails 3.1
我得到異常:
undefined method profile' for #<Class:0x7fe8aa0674b8>
目前我的模型是這樣的:
帳戶::用戶:
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
賬號::簡介
class Account::Profile < ActiveRecord::Base
belongs_to :user, :class_name 'Account::User'
end
我控制器操作是這樣的:
def dashboard
@user = Account::User.profile
end
這很有道理。謝謝Sczizzo。 – Tempname 2011-12-21 21:25:53