Created
April 9, 2010 12:23
-
-
Save randika/361105 to your computer and use it in GitHub Desktop.
Ruby on Rails: How to bypass Model validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Ruby on Rails: How to bypass Model validation | |
#User Model | |
class User < ActiveRecord::Base | |
validates_presence_of :first_name, :last_name | |
end | |
#BusinessAccount Model | |
class BusinessAccount < ActiveRecord::Base | |
validates_uniqueness_of :username | |
validates_presence_of :username, :category, :name | |
end | |
#Signup Controller | |
class SignupController < ApplicationController | |
def new | |
@user = User.new | |
@business_account = BusinessAccount.new | |
end | |
def create | |
@user = User.new(params[:user]) | |
@business_account = BusinessAccount.new(params[:business_account]) | |
if @user.save | |
@business_account.user_id = @user.id | |
@business_account.save_with_validation(false) # This is the line all the magic happens | |
render :action => 'checkout' | |
else | |
render :action => 'new' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment