Reputation: 265
I have a main model called 'Notes' which has the following:
attr_accessible :name, :label_tokens
has_many :labelships
has_many :labels, :through => :labelships
attr_reader :label_tokens
So basically the Note_id & Label_id are kept in the Labelships table.
What i would like to do is create a list of distinct Labels and create a link on each value to the respective Note.
Example: Note 'mynote' has a label of 'git' associated through the labelship table, i would like Git to appear on a list of other labels, and then when i click on git, i get a list of Notes that have the label 'git' on them.
Upvotes: 0
Views: 55
Reputation: 64363
Assuming you have the following models:
class Note
has_many :labelships
has_many :labels, :through => :labelships
end
class Labelships
belongs_to :note
belongs_to :label
end
class Label
has_many :labelships
has_many :notes, :through => :labelships
end
Now given a label, you can get its notes as follows:
label.notes
To exclude the note in hand:
label.notes.where("id != ?", note.id)
Upvotes: 1