class User
  has_one :super_admin
end

class SuperAdmin
  belongs_to :user
end

How would I create a SuperAdmin instance associating a certain user, from withing the User model?

I'm looking for something like this (in the User model), but it's not working:

def promote_to_super
  self.super_admin.create!
end
有帮助吗?

解决方案

You can use create_association(attributes = {}):

def promote_to_super
  self.create_super_admin
end

See more here.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top