jodosha (owner)

Revisions

gist: 117189 Download_button fork
public
Public Clone URL: git://gist.github.com/117189.git
Embed All Files: show embed
app/models/contact.rb #
1
2
3
4
5
class Contact < ActiveRecord::Base
  belongs_to :customer
  belongs_to :project
end
 
app/models/customer.rb #
1
2
3
4
5
class Customer < ActiveRecord::Base
  has_many :contacts
  has_many :projects, :through => :contacts
end
 
app/models/project.rb #
1
2
3
4
class Project < ActiveRecord::Base
  has_many :contacts
end
 
console_commands.rb #
1
2
3
4
5
6
7
8
9
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
# => []
test/fixtures.rb #
1
2
3
4
5
6
customer = Customer.create! :name => "Acme Inc."
project = Project.create! :name => "Rails"
contact = Contact.create! :name => "Bugs Bunny"
project.contacts << contact
customer.contacts << contact