Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

active record test

ActiveRecord testing code

require 'rubygems'
require 'active_record'

ActiveRecord::Base.establish_connection(
  :adapter => "mysql",
  :username => "root",
  :password => "root",
  :database => "blog",
  :host => "localhost" )

class Blog < ActiveRecord::Base 
  set_table_name "posts"
  set_primary_key "id"
end

blog = Blog.find(:all)
blog.each { |row|
  p row
}

A little helper to help you :through

Do you have to code a lot of "has_many :through" relation models in your Rails application? Here is a little helper that will help you DRY. It is very simple indeed, for an example here are the models:
Party, ContectMech and PartyContectMech(the join model).

Call many_to_many_through, perhaps in your application.rb
# many_to_many_through PartyContectMech, Party, ContectMech returns:
#    Party.has_many :party_contact_meches, :foreign_key => :party_id
#    Party.has_many :contact_meches, :through => :party_contact_meches, :foreign_key => :party_id
#
#    ContactMech.has_many :party_contact_meches, :foreign_key => :contact_mech_id
#    ContactMech.has_many :parties, :through => :party_contact_meches, :foreign_key => :contact_mech_id
#
#    PartyContactMech.belongs_to :party
#    PartyContactMech.belongs_to :contact_mech
#
# evaluating the result defines the necessary 6 relations,
# * save some typing, 
# * no need to jump between the 3 source files,
# * less chance for typo and/or human mistake
  eval many_to_many_through(PartyContectMech, Party, ContectMech)



SOURCE:
  def many_to_many_through(through_model, model1, model2)
    through_table = through_model.table_name
    table1 = model1.table_name
    table2 = model2.table_name
    model1_id = model1.name.underscore + "_id"
    model2_id = model2.name.underscore + "_id"
%Q{
    #{model1}.has_many :#{through_table}, :foreign_key => :#{model1_id}
    #{model1}.has_many :#{table2}, :through => :#{through_table}, :foreign_key => :#{model1_id}

    #{model2}.has_many :#{through_table}, :foreign_key => :#{model2_id}
    #{model2}.has_many :#{table1}, :through => :#{through_table}, :foreign_key => :#{model2_id}

    #{through_model}.belongs_to :#{model1.name.underscore}
    #{through_model}.belongs_to :#{model2.name.underscore}
}
  end

Assertions for ActiveRecord validations

These two methods will allow you to assert that an ActiveRecord model should or should not have an validation error.

Put them at the bottom of test/test_helper.rb

  def assert_error_on(field, model)
  	assert !model.errors[field.to_sym].nil?, "No validation error on the #{field.to_s} field."
  end
  
  def assert_no_error_on(field, model)
  	assert model.errors[field.to_sym].nil?, "Validation error on #{field.to_s}."
  end
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS