gpt4 book ai didi

ruby-on-rails - 为什么这会在 Ruby on Rails 3 中返回 Nil?

转载 作者:太空宇宙 更新时间:2023-11-03 17:26:59 25 4
gpt4 key购买 nike

我运行了这个命令:

user1 = User.create(:email => 'test12@abc.com', :password => 'test12123', :username => 'test12', :first_name => 'Test', :last_name => 'User', :plan_id => '4')

得到这个错误:

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

想法?

最佳答案

确保 :plan_id 在用户模型的 attr_accessible 列表中,或者不在 attr_protected 列表中。这将防止在该 User.create 语句中分配 plan_id。

关于ruby-on-rails - 为什么这会在 Ruby on Rails 3 中返回 Nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5691057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com