Question

I am trying to make a relationship between a table called Folders and a table called Media. Folders can contain multiple Media and Media can be contained in multiple Folders. After some research I found that a HABTM relationship is best.

However, am I correct to assume that

  class Media < ActiveRecord::Base
     has_and_belongs_to_many :folders
  end

   class Folders < ActiveRecord::Base
      has_and_belongs_to_many :media
   end

relies on the primary keys.

Although I am using the built in id as my primary key for both Folders and Files, I need to join this table on another attribute (self generated guids/uuids). Is there a way for me to use HABTM on non-primary keys?

Was it helpful?

Solution

class Media < ActiveRecord::Base
  has_and_belongs_to_many :folders, :foreign_key => :column_name
end

class Folders < ActiveRecord::Base
  has_and_belongs_to_many :media, :foreign_key => :column_name
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top