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

About this user

Zeke Sikelianos

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

Rails validation for Phone

  validates_length_of :phone, :is => 10, :message => 'must be 10 digits, excluding special characters such as spaces and dashes. No extension or country code allowed.', :if => Proc.new{|o| !o.phone.blank?}
  validates_length_of :fax, :is => 10, :message => 'must be 10 digits, excluding special characters such as spaces and dashes. No extension or country code allowed.', :if => Proc.new{|o| !o.fax.blank?}

Crack open Firefox's new Places sqlite database with ActiveRecord

require "rubygems"
require "active_record"
require "active_support"

ActiveRecord::Base.establish_connection(
	:adapter => "sqlite3",
	:database => "places.sqlite"
)

class MozHistoryvisit < ActiveRecord::Base
	belongs_to :place, :class_name => "MozPlaces", :foreign_key => "place_id"
end

class MozPlaces < ActiveRecord::Base
end

p Time.now.yesterday
from = Time.now.yesterday.to_i * 1000000

MozHistoryvisit.find(:all, :conditions => ["visit_date > ?", from]).each do |h|
	place = h.place
	puts place.title
	puts place.url
	puts place.visit_count
	puts
end

Find all rows for a certain day; Count days between two dates

# Find all rows created on a certain day; Rails apparently has a built-in :db string format
self.find(:all, :conditions => ["created_at >= ? AND created_at <= ?", day.beginning_of_day.to_s(:db), day.end_of_day.to_s(:db)])

# Find number of days between two dates
def days_between_dates(first, last)
  (YMD(last) - YMD(first))
end

def YMD(date)
  date.to_date.to_s.gsub("-", "").to_i
end

SQL Transaction in Rails

def fetch_value
	sql = ActiveRecord::Base.connection();
	sql.execute "SET autocommit=0";
	sql.begin_db_transaction
	id, value =
	sql.execute("SELECT id, value FROM sometable WHERE used=0 LIMIT 1 FOR UPDATE").fetch_row;
	sql.update "UPDATE sometable SET used=1 WHERE id=#{id}";
	sql.commit_db_transaction
 
	value;
end

Validate uniqueness of an id pair

# Prevent user from joining group twice
def validate
	errors.add_to_base("You are already a member of the #{self.group.name} Group") unless Grouping.find(:all, :conditions => {:user_id => self.user_id, :group_id => self.group_id}).blank?
end

ActiveRecord: Find blog links with futures too distant

badbloglinks = BlogLink.find(:all, :conditions => ["status='reserved' AND expires > ?", Time.now + 2.weeks])

Apply conditions to count in ActiveRecord

blog.posts.count(:conditions => "status = 'active'")

Active Record: Select specific column data

ActiveRecord works at the row (aka record) level, not the column level. That's not to say that you can't just get a single column of data back from the database, you can, but you won't have all the attributes if you do this so just keep that in mind. you'll probably want to map them to an array..
class User < ActiveRecord::Base
  def self.names
    # find all records, then map name attributes to an array
    find(:all, :select => "name").map(&:name)
  end
end

# btw..
.map(&:name)

# is shorthand for
.map { |x| x.name }
« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS