的从属表迁移在我的Rails应用程序我有用户模块,它是依赖于model.Both通过user_role_assignment model.i.e整合角色的用户通过用户角色分配相关联的许多角色。在Ruby中创建
我需要生成一个迁移来建立一些默认的用户。 这个问题我一个面向是,如果我尝试创建通过控制台的用户,我得到一个错误说“角色不能为空
u=User.create(:name => "ramyameena", :email => "[email protected]",:password=>"sandvine",:roles=>{:id=>2,:name=>"Tester"})
=> #<**User id: nil**, name: "ramyameena", created_at: nil, updated_at: nil, email: "[email protected]", encrypted_password: "$2a$10$qIfRLKZlxviag9E0Gzvp8e3VKkOCaXraP7PnJC6vGMN....", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil>
irb(main):012:0> u.errors.inspect
=> "#<OrderedHash **{:roles=>[\"can't be blank\"]**}>"
我的用户模型。
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :name, :presence => true, :uniqueness => true
validates :roles, :presence => true
has_many :user_role_assignments
has_many :roles, :through => :user_role_assignments
has_many :tester_release_assignments
has_many :releases, :through => :tester_release_assignments
has_many :releases
has_many :ic_runs
accepts_nested_attributes_for :user_role_assignments
attr_accessible :email, :name, :password, :password_confirmation, :role_ids
http://stackoverflow.com/questions/5916162/problem-with-factory-girl-association-and-after-initialize –