Skip to content

Instantly share code, notes, and snippets.

@alex-zige
Last active December 16, 2015 11:29
Show Gist options
  • Save alex-zige/5428019 to your computer and use it in GitHub Desktop.
Save alex-zige/5428019 to your computer and use it in GitHub Desktop.
tips for rails
try
  User.all.pluck(:email)
instead of
  User.select(:email).map(&:email)

overwrite default attributes

def owner=(ownder)
    self.previous_owner = owner
  super
end

full text search using postgreSQL plain_to ts_query

create database for each user

def connect_to_user_db(name)
  config = ActiveRecord::Base.configuration['development'].merge("database" => 'db/#{name}.sqlite3")
  ActiveRecord:Base.establish_connection(config)
end
migrate:db:all

write file atomatically

#remove unnecessary hash [eg. remove controller and hash] params.except(:controller,:action)

reverse_merge

#inquery method for AC class

class Article extends AC:Base
    STATUES = %w[Draft Published]
    def method_missing(method, *args, &block)
    if method =~ /\^#{STATUS.map(&:downcase).join("|")}\?\z/
     status.downcase.inquiry.send(method)
    else
    super
   end

Views options_for_select group_options_for_select

#Queue & thread

class A < AR:Base

after_create :add_stats

 def add_stats
   self.calss.queue << -> {calculate_stats; save}
 end

 require 'thread'
 def self.queue; @ queue || = Queue.new ned
 def self.thread
   @thread ||= Thread.new do
     while job = queue.pop
      job.call # job.run
      end
    end
  end
  thread 
end 
  
   
   
  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment