Rails devise create a related object after new user is created?

Adam Bronfin

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"?

Andrew Hendrie

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

create an instance after devise user registration, rails

From Dev

Rails after_create make a related object

From Dev

Devise user created with Rails generator cannot sign in

From Dev

Rails Trying to create a profile after the user has been created

From Dev

Create related objects after initialize another object in rails

From Dev

Rails: Only allow admin user to create new users in Rails with Devise (No external modules)

From Java

Have problem to auto create profile after register new user in django the error show this 'Manager' object has no attribute 'created'

From Dev

Log a user into their subdomain after registration with Rails and Devise

From Dev

Log a user into their subdomain after registration with Rails and Devise

From Dev

Rails 4 - checkbox agreement validation fails on create new user registration using devise

From Dev

Rails 4 - checkbox agreement validation fails on create new user registration using devise

From Dev

Make the Devise new user form created with railapps a partial to re use it

From Dev

Make the Devise new user form created with railapps a partial to re use it

From Dev

Editing user details creates new user. Devise, Rails 4

From Dev

Rails, Devise: If user signed in create with user id, else without

From Dev

Create sample data on devise user create in Ruby on Rails

From Dev

Rails 4: Can't create a session for a user through login, although the session is created when I create a new user

From Dev

Setup a new user after it's created

From Dev

Rails 4 has_many :through association: use Devise current_user in other parent model after_create callback

From Dev

Asking the user to create a new object?

From Dev

Rails 4.0 Devise - Polymorphic - redirect to user after signup

From Dev

How to sign in the user after redirect through devise in Rails?

From Dev

Rails: After devise user_signed_in? have a sitewide validation

From Dev

Rails 4.0 Devise - Polymorphic - redirect to user after signup

From Dev

Rails/Devise - how to logout a user after an `x` minutes of inactivity?

From Dev

How to create new devise user using foreign controller

From Dev

How to create new devise user using foreign controller

From Dev

Create User Account Settings Page in Ruby on Rails with devise

From Dev

How to create fixtures (for a Devise user) as a yml.erb in rails (4.1.5)?

Related Related

  1. 1

    create an instance after devise user registration, rails

  2. 2

    Rails after_create make a related object

  3. 3

    Devise user created with Rails generator cannot sign in

  4. 4

    Rails Trying to create a profile after the user has been created

  5. 5

    Create related objects after initialize another object in rails

  6. 6

    Rails: Only allow admin user to create new users in Rails with Devise (No external modules)

  7. 7

    Have problem to auto create profile after register new user in django the error show this 'Manager' object has no attribute 'created'

  8. 8

    Log a user into their subdomain after registration with Rails and Devise

  9. 9

    Log a user into their subdomain after registration with Rails and Devise

  10. 10

    Rails 4 - checkbox agreement validation fails on create new user registration using devise

  11. 11

    Rails 4 - checkbox agreement validation fails on create new user registration using devise

  12. 12

    Make the Devise new user form created with railapps a partial to re use it

  13. 13

    Make the Devise new user form created with railapps a partial to re use it

  14. 14

    Editing user details creates new user. Devise, Rails 4

  15. 15

    Rails, Devise: If user signed in create with user id, else without

  16. 16

    Create sample data on devise user create in Ruby on Rails

  17. 17

    Rails 4: Can't create a session for a user through login, although the session is created when I create a new user

  18. 18

    Setup a new user after it's created

  19. 19

    Rails 4 has_many :through association: use Devise current_user in other parent model after_create callback

  20. 20

    Asking the user to create a new object?

  21. 21

    Rails 4.0 Devise - Polymorphic - redirect to user after signup

  22. 22

    How to sign in the user after redirect through devise in Rails?

  23. 23

    Rails: After devise user_signed_in? have a sitewide validation

  24. 24

    Rails 4.0 Devise - Polymorphic - redirect to user after signup

  25. 25

    Rails/Devise - how to logout a user after an `x` minutes of inactivity?

  26. 26

    How to create new devise user using foreign controller

  27. 27

    How to create new devise user using foreign controller

  28. 28

    Create User Account Settings Page in Ruby on Rails with devise

  29. 29

    How to create fixtures (for a Devise user) as a yml.erb in rails (4.1.5)?

HotTag

Archive