2017-03-24 3 views
0

3 가지 모델이 있습니다. 소유주, 부동산 및 임대인. 내 부동산 시스템에 세입자가 몇 가지 임대 속성을 가질 수 있기 때문에 나는 Renter 연관을 혼동합니다. 아마도 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 

답변

0

난 당신이에 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