CirclingMinds

June 21, 2007

Validations based on a Controller Method

Filed under: advanced-rails — shovan @ 10:03 pm

Sometimes i have ran into a situation where there was a need to validate a new record depending upon which method was used. For example I was capturing data for an event which had both domestic and international leads. There were different criteria for the two sets of data.

For eg: I needed to validate presence of state, cost center for US leads and ignore those for international leads.

So i needed to figure out separate ways to handle the validations. Here is how I implemented it.

I defined two methods in my model: usa_validation? and international_validation?

  # validations only specific to usa leads
  attr_accessor :usa_validation
 
  def usa_validation?
    return usa_validation
  end
 
  def usa_validation=(value)
    @usa_validation = value
  end
 
  validates_presence_of :region, :if => :usa_validation?
  validates_presence_of :cost_center, :if => :usa_validation?
 
  # validations only specific to international leads
  attr_accessor :international_validation
 
  def international_validation?
    return international_validation
  end
 
  def usa_validation=(value)
    @usa_validation = value
  end
 
  validates_presence_of :country, :if => :international_validation?

In my controller i set the flag to true for US or international depending upon the case.

  def capture_usa_lead
    @lead = Lead.new(params[:lead])
    @lead.usa_validation =  true
    if @lead.save
      #display message
    else 
      render  :layout => "lead", :partial => "usa_form"
    end
  end
  def capture_international_lead
    @lead = Lead.new(params[:lead])
    @lead.international_validation = true
    if @lead.save
            #display message
    else
      render  :layout => "lead", :partial => "international_form"
    end
  end

June 20, 2007

Rake Commands List

Filed under: ruby-on-rails — shovan @ 9:56 pm
rake db:fixtures:load          # Load fixtures into the current environment's database.  Load specific fixtures using FIXTURES=x,y
rake db:migrate                # Migrate the database through scripts in db/migrate. Target specific version with VERSION=x
rake db:schema:dump            # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load            # Load a schema.rb file into the database
rake db:sessions:clear         # Clear the sessions table
rake db:sessions:create        # Creates a sessions table for use with CGI::Session::ActiveRecordStore
rake db:test:clone             # Recreate the test database from the current environment's database schema
rake db:test:clone_structure   # Recreate the test databases from the development structure
rake db:test:prepare           # Prepare the test database and load the schema
rake db:test:purge             # Empty the test database
rake log:clear                 # Truncates all *.log files in log/ to zero bytes
rake rails:freeze:edge         # Lock to latest Edge Rails or a specific revision with REVISION=X (ex: REVISION=4021) or a tag with TAG=Y (ex: TAG=rel_1-1-0)
rake rails:freeze:gems         # Lock this application to the current gems (by unpacking them into vendor/rails)
rake rails:unfreeze            # Unlock this application from freeze of gems or edge and return to a fluid use of system gems
rake rails:update              # Update both configs, scripts and public/javascripts from Rails
rake rails:update:configs      # Update config/boot.rb from your current rails install
rake rails:update:javascripts  # Update your javascripts from your current rails install
rake rails:update:scripts      # Add new scripts to the application script/ directory
rake stats                     # Report code statistics (KLOCs, etc) from the application
rake test                      # Test all units and functionals
rake test:functionals          # Run the functional tests in test/functional
rake test:integration          # Run the integration tests in test/integration
rake test:plugins              # Run the plugin tests in vendor/plugins/**/test (or specify with PLUGIN=name)
rake test:recent               # Test recent changes
rake test:uncommitted          # Test changes since last checkin (only Subversion)
rake test:units                # Run the unit tests in test/unit
rake tmp:cache:clear           # Clears all files and directories in tmp/cache
rake tmp:clear                 # Clear session, cache, and socket files from tmp/
rake tmp:create                # Creates tmp directories for sessions, cache, and sockets
rake tmp:pids:clear            # Clears all files in tmp/pids
rake tmp:sessions:clear        # Clears all files in tmp/sessions
rake tmp:sockets:clear         # Clears all files in tmp/sockets

Powered by WordPress