2011-10-10 68 views
0

我試圖一次創建3個嵌套模型,並且對我的驗證有麻煩。創建嵌套模型 - Rails 3.1

這是我的模型:

class UserEntity < ActiveRecord::Base 
    has_many :users,   :dependent => :restrict, :autosave => true 
end 

class User < ActiveRecord::Base 
    has_many :user_login_services, :dependent => :destroy, :autosave => true 
    belongs_to :user_entity 
end 

class UserLoginService < ActiveRecord::Base 
    belongs_to :user 
    #validates :user_id, :presence => true 
end 

(UserEntity可以是公司,擁有衆多用戶UserLoginService用於Omniauth服務,如Facebook,OpenID的,等等。)

用戶與創建

def new_user_login(tokens) 
    user_entity = UserEntity.new 
    user = user_entity.users.build(:email => tokens[:email], :password => Devise.friendly_token[0,20], :first_name => tokens[:first_name], :last_name => tokens[:last_name], :has_local_password => false) 
    user.skip_confirmation! 
    user.user_login_services.build(:provider => tokens[:provider], :uid => tokens[:uid], :uname => tokens[:name], :uemail => tokens[:email])  
    user_entity.save! 
    user.confirm! 

此代碼似乎工作正常,並在所有3個表中創建適當的條目。當我取消出現的問題「驗證:USER_ID,:存在=>真正的」在UserLoginService,這讓我

ActiveRecord::RecordInvalid (Validation failed: Users user login services user can't be blank): 
    app/controllers/users/omniauth_callbacks_controller.rb:86:in `new_user_login' 

注意,代碼似乎是工作的罰款,而不驗證,並USER_ID在user_login_services表設置爲適當的值。

爲什麼我得到這個錯誤,我該如何解決這個問題?

回答

1

您需要定義accepts_nested_attributes_forinverse_of關係。

class UserEntity < ActiveRecord::Base 
    has_many :users, :dependent => :restrict, :autosave => true, :inverse_of => :users_entity 
    accepts_nested_attributes_for :users 
end 

class User < ActiveRecord::Base 
    has_many :user_login_services, :dependent => :destroy, :autosave => true, :inverse_of => :user 
    belongs_to :user_entity, :inverse_of => :users 
    accepts_nested_attributes_for :user_login_services 
end 

class UserLoginService < ActiveRecord::Base 
    belongs_to :user, :inverse_of => :users_login_services 
    validates :user, :presence => true 
end 

我也切換到validates :user_id假設validates :user要驗證相關user的存在,不只是在UserLoginServiceuser_id

+0

非常感謝,指定了inverse_of爲我做了!否則,當保存Rails會嘗試首先保存嵌套模型(使用嵌套模型belongs_to頂部模型),並且由於尚未創建頂部模型,這會失敗。 – astgtciv

1

一言以蔽之:

在你的模型

class UserEntity < ActiveRecord::Base 
    has_many :users 
    accepts_nested_attributes_for :users 
end 

class User < ActiveRecord::Base 
    belongs_to :user_entity 
    has_many :user_login_services, :dependent => :destroy 
    accepts_nested_attributes_for :user_login_services 
end 

class UserLoginService < ActiveRecord::Base 
    belongs_to :user 
    validates :user_id, :presence => true 
end 

在控制器

def create 
    @user_entity = UserEntity.new(params[:user_entity]) 
    @user_entity.save 
    # To Do: handle redirections on error and success 
end 

,並在您的形式

<%= form_for(@user_entity) do |f| %> 
    <%= f.fields_for :users do |u| %> 
    <%= u.fields_for :user_login_services do |ul| %> 
     <%= ul.select :user_id, @user_entity.users.collect{|u| [u.name, u.id]} %> 
    <% end %> 
    <% end %> 
<% end %> 

此外,我建議你看看: http://railscasts.com/episodes/196-nested-model-form-part-1

相關問題