Deleting HABTM tag associations

If I have a simple tagging system, with three models: posts, tags, and
posts_tags; and I delete a tag, then naturally I would want to delete
any associations in posts_tags that include that tag, right?

But I don’t want to create a model for my posts_tags table; something
tells me Rails has a way of removing records from join tables
automatically. But I don’t know where I would start from for that.
Anybody care to set me on the right path?

Thanks!

Sean C. wrote:

If I have a simple tagging system, with three models: posts, tags, and
posts_tags; and I delete a tag, then naturally I would want to delete
any associations in posts_tags that include that tag, right?

But I don’t want to create a model for my posts_tags table; something
tells me Rails has a way of removing records from join tables
automatically. But I don’t know where I would start from for that.
Anybody care to set me on the right path?

Thanks!

has_many :model :dependent => :destroy

Use the dependent option which will delete the association :slight_smile:

read more here:
http://weblog.rubyonrails.org/2006/4/28/associations-arent-dependent-true-anymore

:dependent => :delete_all

Wait. My application is crashing now:

“Unknown key(s): dependent”

Is my syntax/placement correct?

"
class Tag < ActiveRecord::Base
has_and_belongs_to_many :posts, :dependent => :delete_all
end
"

?

I knew it! Rails is so great.

Thanks both you guys!

sean

Hmm. Looks good. I just have one question:

The docs say this:

:delete_sql - overwrite the default generated SQL used to remove links
between the associated classes with a manual one

So that means that ActiveRecord has ‘default generated SQL used to
remove links between the associated classes’ out of the box already,
right? So maybe I don’t have to do anything!

I will test this.

Sean C. wrote:

Wait. My application is crashing now:

“Unknown key(s): dependent”

Is my syntax/placement correct?

"
class Tag < ActiveRecord::Base
has_and_belongs_to_many :posts, :dependent => :delete_all
end
"

?

I see the has_and_belongs_to_many cannot use the :dependent key, only
the has_many can use it :slight_smile:

You can use then the :delete_sql key :smiley:

Example from the documentation:
has_and_belongs_to_many :active_projects, :join_table =>
‘developers_projects’, :delete_sql =>
‘DELETE FROM developers_projects WHERE active=1 AND developer_id =
#{id} AND project_id = #{record.id}’

Link =>
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

Go down and look at the options you can call with description :slight_smile:

It worked!

Awesome. It deletes associations automatically out of the box.
Sorry to have taken everybody’s time.

Sean C. wrote:

It worked!

Awesome. It deletes associations automatically out of the box.
Sorry to have taken everybody’s time.

You are welcome :smiley:

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