2014-09-03 51 views
0

在我的應用程序建立一個相關的對象跳過驗證,當創建一個新用戶,配置文件自動建立:在Rails的

before_create :build_default_profile 

    protected 

    def build_default_profile 
     build_profile 
     true 
    end 

我想跳過該配置文件驗證,並保持thread-安全。我嘗試了驗證:false和.send(:create_without_callbacks) - 兩者均未成功。我也可以用填充虛擬數據來填充我的配置文件,但這似乎很麻煩。希望得到任何建議。

+0

你不會想保存配置文件,在它沒有數據? – Surya 2014-09-03 10:01:34

+0

也可以寫相同的代碼較短。只是'before_create:build_profile'沒有受保護的方法 – itsnikolay 2014-09-03 10:13:40

回答

1

希望它有助於

# app/models/profile.rb 
class Profile 
    validates :username, presence: true unless new_record? 
end 


# app/models/user.rb 
class User 
    before_create :build_profile 
end