Skip to content

Instantly share code, notes, and snippets.

View thanhcuong1990's full-sized avatar
🏠
Working from home

Cuong Lam thanhcuong1990

🏠
Working from home
View GitHub Profile
@thanhcuong1990
thanhcuong1990 / topic(3).rb
Created July 20, 2014 16:05
Scope examples
# /app/models/topic.rb
class Topic < ActiveRecord::Base
scope :trending, lambda { |num| where('started_trending > ?', 1.day.ago).
order('mentions desc').
limit(num) }
# ...
end
@thanhcuong1990
thanhcuong1990 / posts_controller(8).rb
Last active August 29, 2015 14:04
Scope examples
# /app/controllers/posts_controller.rb
def index
@trending = Topic.trending(7)
# ...
end
@thanhcuong1990
thanhcuong1990 / topic(2).rb
Created July 20, 2014 15:58
Scope examples
# /app/models/topic.rb
class Topic < ActiveRecord::Base
scope :trending,lambda { where('started_trending > ?', 1.day.ago).order('mentions desc') }
# ...
end
# /app/controllers/posts_controller.rb
def index
@trending = Topic.trending.limit(7)
# ...
end
@thanhcuong1990
thanhcuong1990 / topic(1).rb
Created July 20, 2014 15:47
Scope examples
# /app/models/topic.rb
class Topic < ActiveRecord::Base
scope :trending, where('started_trending > ?', 1.day.ago).
order('mentions desc')
end
@thanhcuong1990
thanhcuong1990 / posts_controller(6).rb
Last active August 29, 2015 14:04
Scope examples
# /app/controllers/posts_controller.rb
def index
@trending = Topic.trending.limit(7)
# ...
end
@thanhcuong1990
thanhcuong1990 / post(3).rb
Created July 20, 2014 15:40
Scope examples
# /app/models/post.rb
class Post < ActiveRecord::Base
default_scope order('created_at desc')
# ...
end
# /app/controllers/posts_controller.rb
def index
@posts = current_user.posts.limit(10)
# ...
end
@thanhcuong1990
thanhcuong1990 / post(2).rb
Created July 20, 2014 15:20
Scope examples
# /app/models/post.rb
class Post < ActiveRecord::Base
scope :recent, order('created_at desc')
# ...
end
# /app/controllers/posts_controller.rb
def index
@posts = current_user.posts.recent.limit(10)
#...
end