Question

In my Rails 4 Application, my Developer model has one user, eg:

class Developer < ActiveRecord::Base
  has_one :user, as: :account
  ...
end

And in my User model I have

class User < ActiveRecord::Base
  belongs_to :account, polymorphic: true
end

When the a developer record is created I would like a user to be created automatically that is associated with the developer. Is this possible? Or is there a way to do this following rails guidelines?

Was it helpful?

Solution

It kind of depends on how you are creating a developer and where the attributes for the user are coming from. If the developer attributes are coming from a form, I'd recommend using accepts_nested_attributes_for on the Developer model. Here is some info about how to use nested attributes http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top