Created
August 9, 2014 02:04
-
-
Save mayfer/2404eed843fbec698c39 to your computer and use it in GitHub Desktop.
ActiveRecord includes/join example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_record' | |
require 'pg' | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Base.establish_connection( | |
:adapter => "postgresql", | |
:host => 'ec2-54-204-41-178.compute-1.amazonaws.com', | |
:username => 'bmdjwluxchptuq', | |
:password => 'aEH-cKdr2zoXYUAjI8Xjma5eXK', | |
:database => 'dfq35t7uirbums', | |
:encoding => 'utf8', | |
) | |
class Author < ActiveRecord::Base | |
def to_s | |
"#{first_name} #{last_name}" | |
end | |
end | |
class Book < ActiveRecord::Base | |
belongs_to :author | |
def to_s | |
"#{title}" | |
end | |
end | |
books = Book.includes(:author).where('authors.id > 1000').references(:authors) | |
# books = Book.all | |
books.each do |book| | |
puts book, book.author | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment