Search code examples
ruby-on-railsrubydeviseruby-on-rails-4virtual-attribute

Virtual attributes with Devise in Rails 4


My User model doesn't have a :name field but I want to include a :name field in my sign up form which will be used to create a record of another model in an after_create.

class User < ActiveRecord::Base
  after_create :create_thing

private
  def create_thing
    @thing = Thing.new
    @thing.name = <here's where I need help>
    @thing.save!
  end
end

How can I get the name from the sign up form?


Solution

  • In your model add attr_accessor for name

    attr_accessor :name
    

    This will allow you to add it to a form and use it to generate the proper data in your after_create