2011-12-21 7 views
0

私は現在テスト中のモデルが2つあります。 1つはトップレベルモデル(Account :: User)で、もう1つは関連モデル(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 

私のコントローラのアクションは、次のようになります。あなたがAccount::User.profileを書くとき

def dashboard 
    @user = Account::User.profile 
end 

答えて

1

、あなたはprofileという名前のクラスメソッドにアクセスしようとしています。さて、has_one(と友達)は実際にクラスのインスタンスに関係を設定するので、@user.profileのように使うことができます。

+0

それは理にかなっています。 Sczizzoありがとうございます。 – Tempname

1

Account::User.profileは動作しません。あなたは次のようなことをすることができます:

@user = Account::User.first 
@profile = @user.profile 
関連する問題