Skip to content

Instantly share code, notes, and snippets.

@jhjguxin
jhjguxin / cap.rb
Created June 20, 2012 05:55 — forked from jeanmartin/cap.rb
Kill resque workers using rake task, let god handle the restart
after "deploy:restart", "resque:stop_workers"
namespace :resque do
task :stop_workers, :except => { :no_release => true } do
run "cd #{current_path} && rake RAILS_ENV=#{rails_env} resque:stop_workers"
end
end
@jhjguxin
jhjguxin / establish_connection_model.rb
Created June 20, 2012 09:35
rails has_many through and Multiple databases causing SQLite::BusyException
class User < ActiveRecord::Base
establish_connection "users_#{Rails.env}" # use alternate DB
has_many :memberships
has_many :groups, through => :memberships
end
class Group < ActiveRecord::Base
establish_connection "users_#{Rails.env}" # use alternate DB
has_many :memberships
has_many :users, :through => :memberships
@jhjguxin
jhjguxin / recommend_product.rb
Created June 21, 2012 04:45
mongoid namespace model i18n
class Recommend::RecommendProduct
include Mongoid::Document
field :position, type: String
field :name, type: String
field :image, type: String
field :title, type: String
field :price, type: Float
field :cnt_buy, type: Integer
field :cnt_comment, type: Integer
@jhjguxin
jhjguxin / recommend_user.rb
Created June 25, 2012 12:14
mongoid namespace model demo
# encoding: utf-8
class Recommend::RecommendUser
include Mongoid::Document
field :position, type: String
field :name, type: String
field :body, type: String
field :created_at, type: DateTime
field :updated_at, type: DateTime
#field :users, type: Array
@jhjguxin
jhjguxin / assignment.rb
Created July 2, 2012 12:19
Rails Active Record Validations with relationship table(eg. foreign key)
class Assignment < ActiveRecord::Base
belongs_to :user
belongs_to :cms_role
end
@jhjguxin
jhjguxin / new.html.erb
Created July 3, 2012 06:35
attr_accessor
<%- model_class = @auth_user.class -%>
<div class="page-header">
<h1><%=t '.title', :default => t('helpers.titles.new', :model => model_class.model_name.human,
:default => "New #{model_class.model_name.human}") %></h1>
</div>
<%= simple_form_for([:auth,@auth_user]) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :username %>
@jhjguxin
jhjguxin / _search_form.html.erb
Created July 3, 2012 11:21
rails find model class from controller
<%= form_tag(common_search_url,:class=>"navbar-search") do %>
<%= text_field_tag(:q,params[:q] || I18n.t("menu.search"), :class => "search-query") %>
<!--%- @model_class = params[:controller].capitalize.singularize if params[:controller].capitalize.singularize.class_exists? -%-->
<%- @type_choose = params[:type] if (params.has_key? :type) and params[:type].present? -%>
<%- @model_class = params[:controller].classify.constantize if params[:controller].classify.class_exists? -%>
<%- @model_class = params[:controller].gsub(/\w+([@\s\/]+)/,"").classify.constantize if params[:controller].gsub(/\w+([@\s\/]+)/,"").classify.class_exists? -%>
<% unless @model_class.nil? %>
<%= hidden_field_tag 'model', @model_class.to_s %>
<% if (@model_class.method_defined? :columns) or (@model_class.respond_to? :columns) %>
<!--%= select_tag "type", options_from_collection_for_select(@model_class.columns, "name", "name")%-->
@jhjguxin
jhjguxin / tips.markdown
Created July 5, 2012 09:00
share static between mult rails projects

if you do not have asset server, can not pay to upyun, and s3 also. you can assign this work to system link, then you will get good job. put the link upder every project's public directory

@jhjguxin
jhjguxin / bundle.markdown
Last active October 6, 2015 22:07
tips from francis

which bundle

bundler

Broken bundle install details (1.1rc)

Cleanup then broken commands:

$ rm -rf ~/.bundle/ ~/.gem/                                                
@jhjguxin
jhjguxin / creating-nested-resources-in-ruby-on-rails-3-and-updating-scaffolding-links-and-redirection.markdown
Created July 9, 2012 03:32
Creating nested resources in ruby on rails 3 and updating scaffolding links and redirection