Update selected attributes from a list
class ActiveRecord::Base def transfer_attributes(source, *keys) keys.each do |key| self.send("#{key}=", source[key]) end end end
11380 users tagging and storing useful source code snippets
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
class ActiveRecord::Base def transfer_attributes(source, *keys) keys.each do |key| self.send("#{key}=", source[key]) end end end
class ActiveRecord::Base @@_cached_columns = {} class << self alias :old_columns :columns def columns return @@_cached_columns[table_name] if @@_cached_columns[table_name] @@_cached_columns[table_name] = old_columns end end end
# in environment.rb or some file you require require 'zlib' CGI::Session::ActiveRecordStore::Session.class_eval { class << self def marshal_with_compression(data) Zlib::Deflate.deflate(marshal_without_compression(data)) end def unmarshal_with_compression(data) unmarshal_without_compression(Zlib::Inflate.inflate(data)) end alias_method_chain :marshal, :compression alias_method_chain :unmarshal, :compression end } # in migration def self.up change_column :sessions, :data, :binary end def self.down change_column :sessions, :data, :text end
badbloglinks = BlogLink.find(:all, :conditions => ["status='reserved' AND expires > ?", Time.now + 2.weeks])
blog.posts.count(:conditions => "status = 'active'")
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 }
class User < ActiveRecord::Base # ... def age (Time.now.year - birthday.year) - (turned_older? ? 0 : 1) rescue 0 end def next_birthday birthday.to_time.change(:year => (turned_older? ? 1.year.from_now : Time.now).year) end def turned_older? (birthday.to_time.change(:year => Time.now.year) <= Time.now) end end
class User < ActiveRecord::Base # ... def password ; @password ; end def password=(value) self.password_salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp self.password_sha = self.encrypt_password(value) @password = value end def encrypt_password(cleartext) Digest::SHA256.hexdigest(cleartext + self.password_salt) end def self.authenticate(nickname, password) user = self.find_active_by_nickname(nickname) raise "Username or Password invalid" if user.blank? || user.encrypt_password(password) != user.password_sha return user end end
module Bezurk #:nodoc: module ActiveRecord #:nodoc: module Extensions def to_conditions attributes.inject({}) do |hash, (name, value)| hash.merge(name.intern => value) end end alias :to_conditions_hash :to_conditions end end end ActiveRecord::Base.send(:include, Bezurk::ActiveRecord::Extensions)
require 'rubygems' require 'active_record' require 'simple-rss' require 'open-uri' require 'twitter' #twitter account to post to twitter_email = "yourtwitteremail@bla.com" twitter_password = "secret" #rss feed to post rss_url = "http://yoursite.com/index.xml" rss_user_agent = "http://twitter.com/yourbot" #sqlite db path_to_sqlite_db = "/PATH/TO/db.sqlite" ActiveRecord::Base.logger = Logger.new(STDERR) ActiveRecord::Base.colorize_logging = false ActiveRecord::Base.establish_connection( :adapter => "sqlite3", :dbfile => path_to_sqlite_db ) #uncomment this section the first time to create the table # #ActiveRecord::Schema.define do # create_table :item do |table| # table.column :title, :string # table.column :link, :string # end #end class Item < ActiveRecord::Base def to_s "#{self.title[0..(130-self.link.length)]} - #{self.link}" end end #run the beast rss_items = SimpleRSS.parse open(rss_url ,"User-Agent" => rss_user_agent) for item in rss_items.items Item.transaction do unless existing_item = Item.find(:all, :conditions => ["link=?", item.link]).first twitter ||= Twitter::Base.new(twitter_email, twitter_password) new_item = Item.create(:title => item.title, :link => item.link) twitter.post(new_item.to_s) end end end