Creating nested models - Rails 3.1

南笙酒味 提交于 2019-12-04 05:31:58

问题


I am trying to create 3 nested models at once, and have trouble with my validations.

These are my models:

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 can be a company, with many users. UserLoginService is used for Omniauth services such as Facebook, openID, etc.)

Users are created with

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!

This code seems to be working fine, and creates appropriate entries in all 3 tables. The problem occurs when I uncomment 'validates :user_id, :presence => true' in UserLoginService, which gets me

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'

Note that the code seems to be working fine without the validation, and user_id in the user_login_services table is set to the proper value.

Why am I getting this error, and how do I solve this?


回答1:


In a nutshell:

in your models

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

in your controllers

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

and in your form

<%= 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 %>

Additionally I recommend you check out: http://railscasts.com/episodes/196-nested-model-form-part-1




回答2:


You need to have accepts_nested_attributes_for and inverse_of relations defined.

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

I also switched validates :user_id to validates :user assuming you want to validate the existence of the associated user and not just that the UserLoginService has a user_id.



来源:https://stackoverflow.com/questions/7715293/creating-nested-models-rails-3-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!