Skip to content

Instantly share code, notes, and snippets.

@mayfer
Created August 9, 2014 02:04
Show Gist options
  • Save mayfer/2404eed843fbec698c39 to your computer and use it in GitHub Desktop.
Save mayfer/2404eed843fbec698c39 to your computer and use it in GitHub Desktop.
ActiveRecord includes/join example
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