2013-10-01 62 views
0

這裏是我的用戶模型保存方法不寫入數據庫

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    has_many :personas 
    has_many :stories, through: :persona 

    validates_presence_of :name 

    attr_accessor :name, :default_persona_id 
    after_create :create_first_persona 

private 
    def create_first_persona 
    @persona = Persona.new 
    @persona.user = self 
    @persona.name = self.name 
    if @persona.save 
     make_first_persona_default 
    end 
    end 

    def make_first_persona_default 
    @user = self 
    @user.default_persona_id = @user.personas.first.id 
    @user.save!(:validate => false) 
    end 
end 

它所做的是,在每次創建人物角色一個用戶註冊,然後將這種生活的ID作爲用戶default_persona_id。

一切正常,除了make_first_persona_default。當我在rails控制檯中檢查用戶時,default_persona_id是零。

我on Rails的4

UPDATE

編輯make_first_persona_default到泰倫東的

def make_first_persona_default 
    unless self.update_attribute(:default_persona_id, self.personas.first.id) 
     raise "got an error trying to save persona: #{self.errors.inspect}" 
    end 
    end 

default_persona_id仍然是零

User Load (1.0ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1 
=> #<User id: 13, email: "[FILTERED]", encrypted_password: "[FILTERED]", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2013-10-01 02:09:19", last_sign_in_at: "2013-10-01 02:09:19", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2013-10-01 02:09:19", updated_at: "2013-10-01 02:09:19", default_persona_id: nil> 

這裏是我的,用戶模式。

create_table "users", force: true do |t| 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "default_persona_id" 
    end 

和我用來添加default_persona_id列的遷移。

class AddActivePersonaToUsers < ActiveRecord::Migration 
    def change 
    change_table :users do |t| 
     t.integer :default_persona_id 
    end 
    end 
end 

回答

2

我不知道這條線在User模型做:

attr_accessor :name, :default_persona_id 

您的意思是accessible?以這種方式創建訪問器將覆蓋ActiveRecord訪問器,因此分配default_persona_id將僅設置實例變量@default_persona_id,並且對數據庫不起作用。

+0

啊!讓我試試這個。 –

+0

這就是問題所在! –

1

在現實中 - 我們沒有理由失去語境。 此外 - 而不是創建一個,然後將自己添加爲相關用戶 - 您可以直接在關聯上創建角色,並自動鏈接自己。

我會做這種方式:

private 
    def create_first_persona 
    persona = self.personas.build(:name => self.name) 
    if persona.save! 
     self.update_attribute(:default_persona_id, persona.id) 
    end 
    end 

如果您希望繼續使用這兩種方法的方式,構建將與幫助。 我懷疑你原來的代碼中的問題是你沒有建立在關聯上 - 因此在用戶找到新的角色之前需要重新加載「角色」 。

此外,您不需要按照您的方式取出當前用戶..您已擁有自己,因此只需使用self。例如:

def make_first_persona_default 
    self.default_persona_id = self.personas.first.id 
    self.save!(:validate => false) 
    end 

,甚至更好,你只設置一個屬性...所以使用更新屬性

def make_first_persona_default 
    self.update_attribute(:default_persona_id, self.personas.first.id) 
    end 
+0

由於@persona正在保存第一個片段沒有收到任何錯誤。我想你想測試self.update_attributes? –

+0

更新我的問題以包含更多信息。順便說一句,是update_attributes將繞過名稱驗證? –

+0

不可以。但是到了這個時候,您已經運行並完成了「創建」,因此它應該已經有效。如果它無效 - 在進入after_create鉤子之前代碼會失敗。 –