Has Many :through associations

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 = ?”,
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs