Skip to content

Commit

Permalink
Back to has_many :through, as it should be
Browse files Browse the repository at this point in the history
  • Loading branch information
elsom25 committed Mar 21, 2024
1 parent 6213f17 commit c3622d0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
6 changes: 2 additions & 4 deletions app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ class Category < ApplicationRecord
has_many :children, class_name: "Category", inverse_of: :parent
belongs_to :parent, class_name: "Category", optional: true

has_and_belongs_to_many :properties,
join_table: :categories_properties,
foreign_key: :category_id,
association_foreign_key: :property_friendly_id
has_many :categories_properties, dependent: :destroy
has_many :properties, through: :categories_properties, foreign_key: :property_friendly_id
def property_friendly_ids=(ids)
self.properties = Property.where(friendly_id: ids)
end
Expand Down
10 changes: 4 additions & 6 deletions app/models/property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
class Property < ApplicationRecord
default_scope { order(:name) }

has_and_belongs_to_many :categories,
join_table: :categories_properties,
foreign_key: :property_friendly_id,
association_foreign_key: :category_id
has_many :categories_properties, dependent: :destroy, foreign_key: :property_friendly_id, primary_key: :friendly_id
has_many :categories, through: :categories_properties

has_and_belongs_to_many :property_values,
join_table: :properties_property_values
has_many :properties_property_values, dependent: :destroy
has_many :property_values, through: :properties_property_values

validates :name, presence: true

Expand Down

0 comments on commit c3622d0

Please sign in to comment.