Forum: Ruby on Rails Has Many :through associations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5afd6080ad6ed43eb305e9e9a0309a74?d=identicon&s=25 David Willis (dawillis)
on 2006-04-06 01:43
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?
This topic is locked and can not be replied to.