HABTM Best Practices

I have 2 main objects, UserProfile and Property. Basically, UserProfile should support 3 different property lists (note: each type of list will have additional properties)

Does anyone see something wrong with the following design for this:

class UserProfile < ActiveRecord::Base
  has_many :shortlists
  has_many :booklists
  has_many :proplists
end

class Shortlist < ActiveRecord::Base
  has_and_belongs_to_many :properties
end

class Booklist < ActiveRecord::Base
  has_and_belongs_to_many :properties
end

class Proplist < ActiveRecord::Base
  has_and_belongs_to_many :properties
end

class Property < ActiveRecord::Base
  has_and_belongs_to_many :shortlists
  has_and_belongs_to_many :booklists
  has_and_belongs_to_many :proplists
end

Another way I looked at is to use polymorphism for a Property object, but I'm not sure how there will be more “rail tracks”

+3
source share
2 answers

HABTM is a bit dated and replaced by has_many: through. Also, check out Railscast for polymorphic associations . Ryan Bates does a great job of this.

+1
source

All Articles