4

I've read about many to many relationship in rails 3 and saw that HABTM has been "deprecated", as in one should use has_many :through most of the time.

I saw plenty of examples where the Join Model has a clear name, for example Magazine, Suscriber = Subscriptions.

But in my case I can't find a good name :/ Is there any convention I should be aware of? Top contains 1 or * Ideas, and an Idea can be in 1 or * Tops. Finally is this the best way to this at all?

Here is my code :

class Top < ActiveRecord::Base  
  has_many :???  
  has_many :ideas, :through => :???  
end

class Idea < ActiveRecord::Base  
  has_many :???  
  has_many :ideas, :through => :???  
end

class ??? < ActiveRecord::Base  
  belongs_to :top  
  belongs_to :idea  
end

Also by using a has_many through I don't need to create manually a join table right ?

Thanks for any help :)

Edit :
A top is like a ranking. So a top is a representation of ideas sorted by their votes. An Idea is an idea (in general). Can be for instance a top of best practices for ruby on rails and an idea "use has_many through instead of HABTM".
So a Top contains 1 or * Ideas and an Idea can belong to 1 or * tops. For ideas it's more a belongs_to_many but it doesn't exist in ror.

Pontek
  • 149
  • 11
  • What is a Top, and how does it contain Ideas? – Brandan Jun 18 '12 at 15:42
  • 1
    To be [precise](http://guides.rubyonrails.org/association_basics.html#additional-column-methods) _"The use of extra attributes on the join table in a has_and_belongs_to_many association is deprecated."_ – Stefan Jun 18 '12 at 15:48
  • Your Top (being like a ranking) sounds like a Snapshot if the idea ranking can change over time... sooo maybe Snapshot has many rankings (some calculated number at some point in time), and has many ideas through rankings. An Idea could have many rankings (over time), and sort of has many snapshots through rankings over time. Still not sure I grok your problem definition though... – railsdog Jun 18 '12 at 20:43
  • Hi thx for your reply. Sorry if I can't make myself understood, I'm french :p I'm gonna keep it simple at first (to learn rails and be confortable with it). Forget about votes. A user can create a top (like my favourites tv serie), add ideas to it (here tv serie like lost or breaking bad) and move them (up/down) into the top. – Pontek Jun 19 '12 at 09:25
  • I wouldn't go so far as to say that HABTM relationships are 'deprecated' - can you link the article that says that. HABTM makes creating fixtures much simpler. However only use them if you're sure the relationship wont be needing any attributes tagged on in the future. – Toby 1 Kenobi Aug 01 '18 at 15:51

2 Answers2

3

First of all, does an Idea really have many sub-Ideas? That sounds like an odd relationship that should be normalized / re-considered.

@gabrielhilal makes a good suggestion, but I would recommend backing up a bit and renaming the Idea and Top tables to be a little less opaque. For example, if Top is short for Topic, Idea seems like more of a "comment" on the Topic, Then your through class could be TopicComment or whatnot.

Anyway, I think you should START with renaming these tables to make a little more sense (or explain them to us so we understand) Then proceeding with @gabrielhilal suggestion of just using a combination of the two tables to name the join table — that is common practice unless the third table has its own specific meaning (or has its own attributes), like Product + Sale = Invoice (where invoice is the join table)

MBHNYC
  • 1,268
  • 1
  • 13
  • 25
  • Hi thx for your reply. I just edited my original post to explain the meaning of my models. An Idea doesn't have sub-ideas (at least for now). Yeah maybe IdeaTop is the way to go but I don't find it very elegant :/ – Pontek Jun 18 '12 at 18:52
  • Just to answer to my thread. I'm finally going with something like this : `class LineTop < ActiveRecord::Base belongs_to :top belongs_to :idea attr_accessible :position end` – Pontek Jun 19 '12 at 18:13
1

We need to understand the meaning of Idea and Top, as well as their relationship (why it is many-to-many), in order to suggest you an appropriated name.
However, if you can't find a name with meaning for this class, you can use something like IdeaTop or TopIdea...

gabrielhilal
  • 10,660
  • 6
  • 54
  • 81
  • According to rails convention, it should be IdeaTop in that case, as the table names should be in alphabetical order. Quick googling brought up this page: http://itsignals.cascadia.com.au/?p=7, but I'm sure I've read it in an official source. – emrass Jun 18 '12 at 16:37
  • Hey thx for your reply. I just edited my original post to explain the meaning of my models. Yeah maybe IdeaTop is the way to go but I don't find it very elegant :/ – Pontek Jun 18 '12 at 18:53