Rails在控制台中调用User.all时创建nil记录

时间:2014-01-04 04:34:24

标签: activerecord ruby-on-rails-4

每次我进入rails控制台并调用User.all时,活动记录会创建一个nil记录并保存。我认为它与我的before_save回调有关。我试图让用户输入当前的重量,然后使它的起始重量相同。

模型

class User < ActiveRecord::Base
  has_many :weigh_ins
  has_many :comments

  mount_uploader :attachment, ImageUploader
  before_save :equalize_weights, on: create

  private

  def equalize_weights
    self.starting_weight = self.current_weight
  end

end

users_constroller.rb创建方法

def create
  @user = User.new(user_params)

  respond_to do |format|
    if @user.save
      format.html {redirect_to @user, notice: 'User has been created successfully'}
    else
      format.html {render action: 'new'}
    end
  end
end

Rails控制台

irb(main):001:0> User.all
(0.1ms)  begin transaction
SQL (4.6ms)  INSERT INTO "users" ("created_at", "updated_at") VALUES (?, ?)     [["created_at", Sat, 04 Jan 2014 04:21:21 UTC +00:00], ["updated_at", Sat, 04 Jan 2014    04:21:21 UTC +00:00]]

(1.4ms)提交事务

0 个答案:

没有答案