1 => Возьмите столбец (скажем, location_type
или любой столбец, который может иметь значение между shipping location
, billing location
и contact location
) в Location
модели
class User
has_one :ship_location, -> { where("location_type = ?", 'ship') }, :class_name => "Location", :dependent => :destroy
has_one :bill_location, -> { where("location_type = ?", 'bill') }, :class_name => "Location", :dependent => :destroy
end
class Shop
has_one :ship_location, -> { where("location_type = ?", 'ship') }, :class_name => "Location", :dependent => :destroy
has_one :bill_location, -> { where("location_type = ?", 'bill') }, :class_name => "Location", :dependent => :destroy
has_one :contact_location, -> { where("location_type = ?", 'contact') }, :class_name => "Location", :dependent => :destroy
end
class Location
belongs_to :user
belongs_to :shop
# (has column 'location_type')
end
2 => При создании Location укажите его соответствующее значение (например, ship
, bill
, contact
), например -> Скажем, создайте location
для магазина для loation_type
= bill
Location.create(location_type: 'ship', foo: 'bar' ...)