【发布时间】: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