Skip to content

Instantly share code, notes, and snippets.

@mayfer
Created September 17, 2014 17:43
Show Gist options
  • Save mayfer/d717c0a2610af7e9296b to your computer and use it in GitHub Desktop.
Save mayfer/d717c0a2610af7e9296b to your computer and use it in GitHub Desktop.
ActiveRecord 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
validates :first_name, presence: true
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('author_id > ?', 1000).references(:authors)
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