rails many to many self join

后端 未结 4 2052

could some one point me to the right direction:

I try to build a model for rails that build up the following:

ClassA -id

ClassA has a relation to

4条回答
  •  情深已故
    2020-11-30 04:45

    If it doesn't make too much sense to create another class to join the two, an alternative approach could be:

    class Word < ActiveRecord::Base 
      has_and_belongs_to_many :synonyms, class_name: "Word", 
                                         join_table: "word_synonyms",
                                         association_foreign_key: "synonym_id"
    end
    

    The join table would look like this:

    create_table :word_synonyms do |t|
      t.integer :word_id
      t.integer :synonym_id
    end
    

提交回复
热议问题