Skip to content

Instantly share code, notes, and snippets.

@moustafasamir
moustafasamir / user.rb
Created March 5, 2013 13:27
Select nth element only(not all elements) sorted by a specific column
last_post = Post.limit(1).offset(2999).order("created_at desc")
@moustafasamir
moustafasamir / Association Extensions.rb
Last active December 11, 2015 09:18
If you have an extension that should be shared by many associations, you can use a named extension module. For example:
module FindRecentExtension
def find_recent
where("created_at > ?", 5.days.ago)
end
end
class Customer < ActiveRecord::Base
has_many :orders, :extend => FindRecentExtension
end
@moustafasamir
moustafasamir / Association Extensions.rb
Last active December 11, 2015 09:19
You’re not limited to the functionality that Rails automatically builds into association proxy objects. You can also extend these objects through anonymous modules, adding new finders, creators, or other methods. For example:
class Customer < ActiveRecord::Base
has_many :orders do
def find_by_order_prefix(order_number)
find_by_region_id(order_number[0..2])
end
end
end
@moustafasamir
moustafasamir / gist:4250738
Created December 10, 2012 14:05
get facebook friends with picture using Koala
def get_facebook_friends(access_token)
@graph = Koala::Facebook::API.new(access_token)
friends = @graph.get_connections("me", "friends?fields=id,name,picture.type(large)")
return friends
end