2011-04-17 83 views
1

我跑這個命令:爲什麼在Ruby on Rails 3中返回Nil?

,並得到這個錯誤:

NoMethodError: undefined method `trial_duration' for nil:NilClass 

這是我的用戶模型:

# == Schema Information 
# Schema version: 20110412170916 
# 
# Table name: users 
# 
# id     :integer   not null, primary key 
# email    :string(255) 
# encrypted_password :string(128) 
# password_salt  :string(255) 
# reset_password_token :string(255) 
# remember_token  :string(255) 
# remember_created_at :datetime 
# sign_in_count  :integer 
# current_sign_in_at :datetime 
# last_sign_in_at  :datetime 
# current_sign_in_ip :string(255) 
# last_sign_in_ip  :string(255) 
# username    :string(255) 
# first_name   :string(255) 
# last_name   :string(255) 
# created_at   :datetime 
# updated_at   :datetime 
# invitation_token  :string(60) 
# invitation_sent_at :datetime 
# plan_id    :integer 
# current_state  :string(255) 
# confirmation_token :string(255) 
# confirmed_at   :datetime 
# confirmation_sent_at :datetime 
# space_used   :integer   default(0), not null 
# failed_attempts  :integer   default(0) 
# unlock_token   :string(255) 
# locked_at   :datetime 
# trial_end_date  :date 
# active_subscription :boolean 
# 

belongs_to :plan 

before_create :set_trial_end 

def set_trial_end 
    plan = self.plan 
    end_of_trial = self.created_at + plan.trial_duration.days 
    self.trial_end_date = end_of_trial 
end 

這是我的計劃模式:

# == Schema Information 
# Schema version: 20110412101615 
# 
# Table name: plans 
# 
# id     :integer   not null, primary key 
# name    :string(255) 
# storage    :float 
# num_of_projects  :integer 
# num_of_clients  :integer 
# cached_slug   :string(255) 
# created_at   :datetime 
# updated_at   :datetime 
# amount    :integer 
# trial_duration  :integer 
# trial_duration_unit :string(255) 
# currency   :string(255) 
# billing_cycle  :integer 
# billing_cycle_unit :string(255) 
# 

class Plan < ActiveRecord::Base 

    has_many :users 
    has_many :subscriptions 
    has_friendly_id :name, :use_slug => true 

end 

想法?

回答

2

請確保:plan_id位於您的用戶模型的attr_accessible列表中,或者不在attr_protected列表中。這將阻止在該User.create語句中分配plan_id。

+0

這是人。多謝,夥計! – marcamillion 2011-04-17 04:01:02

1

試試這個比較:

plan = Plan.find(4) 
user1 = User.create(:email => '[email protected]', :password => 'test12123', :username => 'test12', :first_name => 'Test', :last_name => 'User', :plan => plan) 

這就是沒有找到相關的「計劃」對象故障所致。

+0

Simon,沒有運氣:('ruby-1.9.2-p0> plan =># ruby​​-1.9.2-p0> user1 = User.create(:email =>'[email protected]',::password =>'test12123',:username =>'test12',:first_name =>'Test',:last_name =>'User',:plan => plan) NoMethodError:undefined method'trial_duration'for nil:NilClass' – marcamillion 2011-04-17 02:47:10

1

您是否嘗試過明確設置關聯?類似於西蒙說,但這樣的:

plan = Plan.find(4) 
user = [code to create the user, but without the "plan" or "plan_id" attribute] 
user.plan = plan 
user.save 

或者,在你的代碼,嘗試做:plan_id => 4代替'4'

+0

This works ...但只有if我註釋掉了'before_create:set_trial_end'。如果我不這樣做,它不允許我保存。 – marcamillion 2011-04-17 03:06:50

1

in before_create :set_trial_end該計劃尚未取得。

我想你會需要def set_trial_end

def set_trial_end 
    plan = Plan.find(plan_id) 
    end_of_trial = self.created_at + plan.trial_duration.days 
    self.trial_end_date = end_of_trial 
end 

你可能會需要一個大幹快上的設置創建一個虛擬屬性,叫做plan_id的數據類型手動獲取計劃。

+0

我試過了,但是我得到了這個:'ActiveRecord :: Reco rdNotFound:無法找到沒有身份證的計劃 – marcamillion 2011-04-17 03:45:49

+0

如果我將此更改爲「plan = Plan.find(4)」,那麼這個功能很棒。但是用'plan_id'。沒有這樣的運氣。 – marcamillion 2011-04-17 03:46:18

1

也許你必須添加trial_duration到你的attr_accessible。

試試這個:

class Plan < ActiveRecord::Base 
has_many :users 
has_many :subscriptions 
has_friendly_id :name, :use_slug => true 
attr_accessible :trial_duration 
end 

這是由於這樣的事實軌從質量分配自動保護。

+0

這看起來像一個有趣的,但我仍然有這個錯誤:'ActiveRecord :: RecordNotFound:找不到沒有ID的計劃' – marcamillion 2011-04-17 03:50:36