Has Many :through associations


#1

Greetings again:

I’m simplifying the case I presented earlier regarding the use of
has_many :through. I’m using DHH’s example from “Persuing Beauty with
Ruby On Rails”.

Here are his models:

class Author < ActiveRecord::Base
has_many :authorships
has_many :books, :through => :authorships
end

class Book < ActiveRecord::Base
has_many :authorships
has_many :authors, :through => :authorships
end

class Authorship < ActiveRecord::Base
belongs_to :author
belongs_to :book
end

Here’s the code sample he provides to exercise the new functionality:

david, awrd = Author.find(1), Book.find(1)
david.authorships.create( :book => awrd,
:contribution => “partial”,
:written_in => “Denmark”
)

david.authorships.first.written_in # => “Denmark”
david.books.first # => awrd

Ok, fine and good. So how do I remove the created authorship? The only
way I have found is this:

Authorship.find(:first,
:conditions => [“author_id = ? and book_id = ?”,
david.id, awrd.id]).destroy

That doesn’t seem like the “Rails” way of doing things. I hoping that I
am missing something. Can anyone help me?