简体   繁体   English

为什么在Ruby on Rails 3中返回Nil?

[英]Why does this return Nil in Ruby on Rails 3?

I ran this command: 我运行了以下命令:

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

and got this error: 并得到此错误:

NoMethodError: undefined method `trial_duration' for nil:NilClass

This is my User model: 这是我的用户模型:

# == 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

This is my Plan model: 这是我的计划模型:

# == 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

Thoughts ? 有什么想法吗?

Make sure :plan_id is in your attr_accessible list on your User model, or is NOT on your attr_protected list. 确保:plan_id在用户模型的attr_accessible列表中,或者不在attr_protected列表中。 This will prevent plan_id from being assigned in that User.create statement. 这将防止在该User.create语句中分配plan_id。

Try this rather: 尝试这样:

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

It is failing due to not finding the associated "plan" object. 由于找不到关联的“计划”对象而失败。

Have you tried setting the association explicitly? 您是否尝试过明确设置关联? Similar to what Simon said, but like: 类似于Simon所说的,但是喜欢:

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

Or, in your code, try doing :plan_id => 4 instead of '4' . 或者,在您的代码中,尝试执行:plan_id => 4而不是'4'

in before_create :set_trial_end the plan has not been fetched yet. before_create :set_trial_end ,尚未提取计划。

I think you would need to manually fetch plan in def 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

You will probably need a virtual attribute called plan_id that gets set on the create. 您可能需要在创建时设置的名为plan_id的虚拟属性。

maybe you have to add trial_duration to your attr_accessible. 也许您必须在您的attr_accessible中添加trial_duration。

Try this: 尝试这个:

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

This is due the fact rails auto-protect from mass assignments. 这是由于Rails可以自动保护免受大规模分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM