Skip to content

Instantly share code, notes, and snippets.

@jodosha
Created May 24, 2009 17:12
Show Gist options
  • Save jodosha/117189 to your computer and use it in GitHub Desktop.
Save jodosha/117189 to your computer and use it in GitHub Desktop.
load "test/fixtures.rb"
customer = Customer.first
# => #<Customer id: 1, name: "Acme Inc.", created_at: "2009-05-24 17:05:45", updated_at: "2009-05-24 17:05:45">
customer.projects
# => [#<Project id: 1, name: "Rails", created_at: "2009-05-24 17:05:45", updated_at: "2009-05-24 17:05:45">]
customer.projects.destroy_all
# => []
Project.all
# => []
class Contact < ActiveRecord::Base
belongs_to :customer
belongs_to :project
end
class Customer < ActiveRecord::Base
has_many :contacts
has_many :projects, :through => :contacts
end
customer = Customer.create! :name => "Acme Inc."
project = Project.create! :name => "Rails"
contact = Contact.create! :name => "Bugs Bunny"
project.contacts << contact
customer.contacts << contact
class Project < ActiveRecord::Base
has_many :contacts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment