Has and belongs to many

Hi everyone.

I’ve got the following in my model:

class Newsletter < ActiveRecord::Base
has_and_belongs_to_many :tags

def tag_attributes=(tag_attributes)
tag_attributes.each do |attributes|
tags.build(attributes)
end
end
end

class Tag < ActiveRecord::Base
has_and_belongs_to_many :newsletters
end

And I also got a newsletters_tags relationship table in my DB,
containing newsletter_id and tag_id (both fields are set as
primary_key)

How do I perform a search to retrieve every tag within a particular
newsletter, or all newsletters that relates to an specific tag?

Daniel D. wrote:

How do I perform a search to retrieve every tag within a particular
newsletter,

my_newsletter.tags

or all newsletters that relates to an specific tag?

my_tag.newsletters

@tag.newsletters
or
@newsletter.tags

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #3 out NOW!
http://sensei.zenunit.com/

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