Skip to content

Instantly share code, notes, and snippets.

@jonleighton
Created May 10, 2013 12:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonleighton/5554056 to your computer and use it in GitHub Desktop.
Save jonleighton/5554056 to your computer and use it in GitHub Desktop.
gem "rails", "~> 3.2.0"
require "active_record"
require "logger"
ActiveRecord::Base.establish_connection(
adapter: "sqlite3",
database: ":memory:"
)
puts ActiveRecord::VERSION::STRING
ActiveRecord::Schema.define do
create_table :manufacturers
create_table :products do |t|
t.integer :manufacturer_id
t.string :name
end
end
class Manufacturer < ActiveRecord::Base
has_many :products, :inverse_of => :manufacturer
end
class Product < ActiveRecord::Base
belongs_to :manufacturer, :inverse_of => :products
def self.by_name
order(:name)
end
end
m = Manufacturer.create
m.products.create
ActiveRecord::Base.logger = Logger.new(STDERR)
Manufacturer.first.products.first.manufacturer
puts "--"
products = Manufacturer.first.products
p products.method(:first).source_location
p products.method(:where).source_location
products.where("1=1").first.manufacturer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment