Skip to content

Instantly share code, notes, and snippets.

@underwhelmed
Created March 13, 2012 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save underwhelmed/2030911 to your computer and use it in GitHub Desktop.
Save underwhelmed/2030911 to your computer and use it in GitHub Desktop.
Example with wicked gem on create?
class Account < ActiveRecord::Base
devise :database_autheticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
has_one :username
has_one :subscription
attr_accessible :email, :password, :password_confirmation, :remember_me, :full_name, :address_1, :address_2, :city, :state_province, :postal_code, :country
end
class AccountController < Wicked::WizardController
steps :start, :info, :address, :billing, :review
def show
end
end
class Subscription < ActiveRecord::Base
belongs_to :plan
belongs_to: :account
end
class UserName < ActiveRecord::Base
belongs_to :account
accepts_nested_attributes_for :account
end
@underwhelmed
Copy link
Author

In this case, I don't want to create the account until I get all information, starting with the username (step :start), account information (steps :info and :address) and subscription information (step :billing).

@schneems
Copy link

So we need to store the data the user is input, but either keep a record that this is incomplete data in the system, or not save incomplete data.

Option 1

You could add a status field to your table then have a status of 'pending' and you can change all your validations to only run :if => pending? You can then have a sweeper that goes back and deletes all pending accounts that haven't been finalized for over 6 hours etc. This lets you save all of your data in the table, apply any column specific validations, and gives you full control over the process

Option 2

You can store all the values you need either in another table or in the session, or a data store such as redis/memecache/etc., though if you do this...you don't get any validations on your data. You then wait till the last step to actually save the data to the table.

It is much easier and much cleaner to store the data in the correct table, but with some kind of status/flag field to indicate that the account is not complete. I would recommend option 1.

@underwhelmed
Copy link
Author

I like the pending idea. thanks!

@schneems
Copy link

happy to help !

^_^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment