【发布时间】:2011-04-26 18:36:47
【问题描述】:
我从多年的 c# 和 MSSQL 中学习 RoR。
我选择了一个项目来为我的出租物业经理兄弟建立一个网站。我认为这应该相当容易,因为模型应该是直截了当的,但它认为我可能想太多了,或者我很难放弃“旧”的方式。无论如何,这是问题所在。我从两个模型(用户和属性)开始。属性模型很简单,用户不多。我认为我们在系统中有三种类型的用户。租户、业主和经理(我的兄弟将是唯一的经理,但我想我会设计它来发展)他为几个业主管理房产,每个业主都可以拥有许多房产。每个物业将有一名业主、一名租户和一名经理。
租户将能够登录并看到他们租用的房产,可能会填写维护请求或类似的东西......(此时甚至没有真正要求让租户登录系统,但我认为它会做个好运动)
所有者也是如此,他们都不需要访问系统(他们雇用了我的兄弟,所以他们不必参与其中),但我认为这可能是一个很好的练习。
我使用 Nifty_generator 生成一个用户,它只提供电子邮件、密码等。我将其扩展如下......
class AddProfileDataToUsers < ActiveRecord::Migration
def self.up
add_column :users, :first_name, :string
add_column :users, :last_name, :string
add_column :users, :address1, :string
add_column :users, :address2, :string
add_column :users, :city,:string
add_column :users, :state, :string
add_column :users, :zip, :string
add_column :users, :phone, :string
add_column :users, :email, :string
add_column :users, :user_type, integer
end
def self.down
remove_column :users, :first_name
remove_column :users, :last_name
remove_column :users, :address1
remove_column :users, :address2
remove_column :users, :city
remove_column :users, :state
remove_column :users, :zip
remove_column :users, :phone
remove_column :users, :email
remove_column :users, :user_type
end
end
这里是创建属性表的代码
class CreateProperties < ActiveRecord::Migration
def self.up
create_table :properties do |t|
t.string :address
t.string :city
t.string :type
t.integer :beds
t.float :baths
t.float :price
t.float :deposit
t.string :terms
t.string :laundry
t.datetime :date_available
t.integer :sqft
t.integer :owner_id
t.integer :manager_id
t.integer :tenant_id
t.timestamps
end
end
def self.down
drop_table :properties
end
end
我在 nifty_authentication 生成器生成的用户模型中添加了以下内容
class User < ActiveRecord::Base
#other stuff in the user model up here......
validates_length_of :password, :minimum => 4, :allow_blank => true
#this is the stuff that I have added to the user model
has_many :managed_properties, :class_name => "Property", :foreign_key => "manager_id"
has_many :owned_properties, :class_name => "Property", :foreign_key => "owner_id"
has_one :rented_property, :class_name => "Property", :foreign_key => "tenant_id"
然后我将它添加到属性模型中......
class Property < ActiveRecord::Base
belongs_to :manager, :class_name => "User" #picked up by the manager_id
belongs_to :owner, :class_name => "User" #picked up by the owner_id
belongs_to :tenant, :class_name => "User" #picked up by the tenant_id
end
我的问题是,这看起来像是一种可以接受的方式来模拟我所描述的情况吗?
我应该使用单表继承并创建租户模型吗?经理模型;和所有者模型?我看到这样做的问题是单个用户可能既是经理又是所有者。这可以通过为用户创建一个角色表来解决,其中一个用户有很多角色,一个角色有很多用户。我还查看了一个与用户表一对一匹配的配置文件表并使其具有多态性,但我不认为这种情况真的需要这样做,它并没有解决用户可以成为所有者的问题还有一个经理.....
这是我开始认为也许是我过度思考问题并想出了你在此处看到的内容的时候。
我欢迎您提出任何建设性的意见。请记住,我实际上从未在 Rails 中构建过任何东西,这只是第一次尝试,一周前我什至从未在我的计算机上安装过 Rails。
我不知道这是否重要,但我认为管理员/经理将负责创建用户。这将不是一个自我注册类型的网站。经理在注册新所有者时将添加新所有者,租户也是如此。这将更容易确定他正在创建的用户类型。
感谢您提供的任何见解。
【问题讨论】:
标签: ruby-on-rails model