Using Rails 4.1 and Devise 3.0.3, how can I create an associated object on User when User is instantiated and connect the two?
def User
has_one :case
end
def Case
belongs_to :user
end
In this case, User is set up with devise.
What I'd like to do is instantiate an object like so:
@case = Case.new
current_user.case = @case
or
current_user.case << @case
How can I execute this code when a request is made to "registrations#new"?
Here is how I would implement this:
class User < ActiveRecord::Base
...
before_action :create_case, only: [:new, :create]
def create_case
case = Case.create
self.case = case.id
# Maybe check if profile gets created and raise an error
# or provide some kind of error handling
end
end
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments