Forum: Ruby on Rails Has Many :through associations

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
David W. (Guest)
on 2006-04-06 03: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

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

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

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:

  :conditions => ["author_id = ? and book_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.