How best to associate an Address to multiple models in rails?

前端 未结 2 2158
余生分开走
余生分开走 2021-02-20 02:31

This question on SO appears to be related to my question, but I\'m not sure my question is answered by that.

An address can belong to more than one model (UserProfile an

2条回答
  •  长情又很酷
    2021-02-20 03:34

    One way would be standard Rails polymorphism:

    class Address
      belongs_to :addressable, :polymorphic => true
    end
    
    class UserProfile
      has_one address, :as => :addressable
    end
    
    class Event
      has_one address, :as => :addressable
    end
    

    It could be the nagging feeling you have about this is that you can't create a db-level constraint with Rails-style polymorphic relationships. The alternative (suggested by Dan Chak in Enterprise Rails) is like your #1 option, where you do indeed create a separate id field for each type of relationship. This does leave unused fields, but it also allows for constraints. I can see the argument for both, but the Rails community has been using AR polymorphism for some time with apparently good success. I use it without hesitation. But if that bugs you, you can use Chak's approach. It's a lot more work. : )

    EDIT: @Slick86, migration would look something like:

    class CreateAddresses < ActiveRecord::Migration
      def change
        create_table :addresses do |t|
          t.integer :addressable_id
          t.string :addressable_type
        end
      end
    end
    

提交回复
热议问题