【发布时间】:2011-11-09 20:40:06
【问题描述】:
我有一个简单的用户模型
class User < ActiveRecord::Base
has_one :user_profile
end
还有一个简单的 user_profile 模型
class UserProfile < ActiveRecord::Base
belongs_to :user
end
问题是当我调用以下构建方法时,没有调用保存方法,我最终会在数据库中获得一条新记录(如果它通过验证)
class UserProfilesController < ApplicationController
def create
@current_user = login_from_session
@user_profile = current_user.build_user_profile(params[:user_profile])
#@user_profile.save (even with this line commented out, it still save a new db record)
redirect_to new_user_profile_path
end
Anyyyyyy 任何人都知道发生了什么。
此方法的定义如下,但它仍然为我保存
build_association(attributes = {})
Returns a new object of the associated type that has been instantiated with attributes and linked to this object through a foreign key, but has not yet been saved.
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_one
【问题讨论】:
标签: ruby ruby-on-rails-3 activerecord