2011-08-14 40 views
0

我有一個用戶&個人資料模型。用戶擁有一個配置文件IE。Rails 3檢查關係計數

class User < ActiveRecord::Base 
    has_secure_password 

    # Relationships 
    has_one :profile 

class Profile < ActiveRecord::Base 

    # Relationships 
    belongs_to :user 

然後我試圖測試用戶是否有配置文件。如果沒有重定向到配置文件控制器即。

class User::BaseController < ApplicationController 

    # Filters 
    before_filter :check_for_profile 

    # Layout 
    layout "backend" 

    # Helpers 
    helper_method :current_user 

    private 

    def current_user 
    @current_user ||= User.find(session[:user_id]) if session[:user_id] 
    end 

    def check_for_profile 
    if current_user.profile.empty? 
     redirect_to new_user_profile_path, :notice => "Please create a profile first." 
    end 
    end 

end 

無論我嘗試什麼,我都會收到錯誤消息。

You have a nil object when you didn't expect it! 
You might have expected an instance of Array. 
The error occurred while evaluating nil.empty? 

我很確定我的關係是正確的。任何人都可以闡明我做錯了什麼?

預先感謝您。

Lee

回答

1

嘗試profile.blank?代替。 empty?未定義爲nil