【问题标题】:About associations关于协会
【发布时间】:2017-03-24 00:29:38
【问题描述】:

我有 3 个模型。业主、财产和承租人。 我对租户协会感到困惑,因为在我的房地产系统中,租户可以拥有多个租赁物业。 也许 has_many through ?如是。如何实现?

class Proprietor < ApplicationRecord
  has_many :properties, dependent: :destroy
end

class Property < ApplicationRecord
  belongs_to :proprietor
end

class Renter < ApplicationRecord
end

class CreateProprietors < ActiveRecord::Migration[5.0]
  def change
    create_table :proprietors do |t|
      t.string :full_name
      t.string :email
      t.date :birthday
      t.string :social_security
      t.string :doc_id
      t.text :address
      t.string :zip_code

      t.timestamps
    end
  end
end

class CreateProperties < ActiveRecord::Migration[5.0]
  def change
    create_table :properties do |t|
      t.references :proprietor, foreign_key: true
      t.text :address
      t.string :zip_code
      t.integer :rooms
      t.integer :bedrooms
      t.integer :bathrooms
      t.integer :garage
      t.string :price
      t.boolean :published, defalt: false

      t.timestamps
    end
  end
end

class CreateRenters < ActiveRecord::Migration[5.0]
  def change
    create_table :renters do |t|
      t.string :full_name
      t.string :email
      t.date :birthday
      t.string :social_security
      t.string :doc_id

      t.timestamps
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails model-associations


    【解决方案1】:

    我不认为你需要一个 has_many 通过这个(除非房产可以有很多租房者以及租房者有很多房产?)

    如果需要如您在问题中所述,这应该是可以实现的:

    class Proprietor < ApplicationRecord
      has_many :properties, dependent: :destroy
    end
    
    class Property < ApplicationRecord
      belongs_to :proprietor
      belongs_to :renter
    end
    
    class Renter < ApplicationRecord
      has_many :properties
    end
    
    class CreateProprietors < ActiveRecord::Migration[5.0]
      def change
        create_table :proprietors do |t|
          t.string :full_name
          t.string :email
          t.date :birthday
          t.string :social_security
          t.string :doc_id
          t.text :address
          t.string :zip_code
    
          t.timestamps
        end
      end
    end
    
    class CreateProperties < ActiveRecord::Migration[5.0]
      def change
        create_table :properties do |t|
          t.references :proprietor, foreign_key: true
          t.references :renter, foreign_key: true
          t.text :address
          t.string :zip_code
          t.integer :rooms
          t.integer :bedrooms
          t.integer :bathrooms
          t.integer :garage
          t.string :price
          t.boolean :published, default: false
    
          t.timestamps
        end
      end
    end
    
    class CreateRenters < ActiveRecord::Migration[5.0]
      def change
        create_table :renters do |t|
          t.string :full_name
          t.string :email
          t.date :birthday
          t.string :social_security
          t.string :doc_id
    
          t.timestamps
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多