Skip to content

Instantly share code, notes, and snippets.

@randika
Created April 9, 2010 12:23
Show Gist options
  • Save randika/361105 to your computer and use it in GitHub Desktop.
Save randika/361105 to your computer and use it in GitHub Desktop.
Ruby on Rails: How to bypass Model validation
#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