Created
September 17, 2014 17:43
-
-
Save mayfer/d717c0a2610af7e9296b to your computer and use it in GitHub Desktop.
ActiveRecord 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 | |
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