Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Cuong Lam thanhcuong1990

🏠
Working from home
View GitHub Profile
View XXXXXXXXXXXXX_add_authentication_token_to_users.rb
# db/migrate/XXXXXXXXXXXXX_add_authentication_token_to_users.rb
class AddAuthenticationTokenToUsers < ActiveRecord::Migration
def change
add_column :users, :authentication_token, :string
add_index :users, :authentication_token, :unique => true
end
end
@thanhcuong1990
thanhcuong1990 / nested_attributes(2).rb
Created Jul 25, 2014
Nested attributes example 2
View nested_attributes(2).rb
# /app/models/user.rb
class User < ActiveRecord::Base
has_one :account_setting, :dependent => :destroy
accepts_nested_attributes_for :account_setting
end
# /app/controllers/users_controller.rb
class UsersController < ApplicationController
def new
@thanhcuong1990
thanhcuong1990 / nested_attributes(1).rb
Last active Aug 29, 2015
Nested attributes example 1
View nested_attributes(1).rb
# /app/models/user.rb
class User < ActiveRecord::Base
has_one :account_setting, :dependent => :destroy
end
# /app/controllers/users_controller.rb
class UsersController < ApplicationController
def create
@user = User.new(params[:user])
@thanhcuong1990
thanhcuong1990 / posts_controller(14).rb
Created Jul 25, 2014
Fantastic filters example 4
View posts_controller(14).rb
# /app/controllers/posts_controller.rb
class PostsController < ApplicationController
def edit
@post = get_post(params[:id])
# ...
end
def update
@post = get_post(params[:id])
# ...
@thanhcuong1990
thanhcuong1990 / posts_controller(13).rb
Created Jul 25, 2014
Fantastic filters example 3
View posts_controller(13).rb
# /app/controllers/posts_controller.rb
class PostsController < ApplicationController
before_filter :get_post, :only => [:edit, :update, :destroy]
def edit
# ...
end
def update
# ...
@thanhcuong1990
thanhcuong1990 / posts_controller(12).rb
Created Jul 25, 2014
Fantastic filters example 2
View posts_controller(12).rb
# /app/controllers/posts_controller.rb
class PostsController < ApplicationController
before_filter :get_post, :only => [:edit, :update, :destroy]
def get_post
@post = Post.find(params[:id])
end
def edit
# ...
@thanhcuong1990
thanhcuong1990 / posts_controller(11).rb
Created Jul 25, 2014
Fantastic filters example 1
View posts_controller(11).rb
# /app/controllers/posts_controller.rb
class PostsController < ApplicationController
def edit
@post = Post.find(params[:id])
# ...
end
def update
@post = Post.find(params[:id])
# ...
View posts_controller(10).rb
current_user.posts.create(
:title => "This is the title",
:content => "This is the content of post"
)
View posts_controller(9).rb
# /app/controllers/posts_controller.rb
p = Post.new
p.title = "This is the title"
p.content = "This is the content of post"
p.user = current_user
p.save
View topic(4).rb
# /app/models/topic.rb
class Topic < ActiveRecord::Base
scope :trending, lambda { |num = nil| where('started_trending > ?', 1.day.ago).
order('mentions desc').
limit(num) }
# ...
end
You can’t perform that action at this time.