Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby On Rails User Model for multiple types
    text
    copied!<p>I am learning RoR coming from many years of c# and MSSQL.</p> <p>I have picked a project to build a web site for my brother who is a rental property manager. I figured this should be fairly easy as the models should be straight forward, but it think I may be over thinking everything or I’m having trouble letting go of the ‘old’ way. Anyway here is the problem. I am starting off with just two models (User and Property) . The property model is easy, the user not so much. I figured that we have three types of users in the system. Tenants, Owners and Managers (my brother will be the only manager but I figured I would design it to grow) He manages properties for several owners each of whom can own many properties. Each property will have one owner, one tenant and one manger.</p> <p>Tenants will be able to log in and just see the property they rent to maybe fill out a maintenance request or something like that…(no real requirement at this point to even give tenant a login to the system but I thought it would be a good exercise)</p> <p>Same thing goes for owners, none of them really need access to the system (they hire my brother so they don’t have to be involved) but I thought it might be nice and again a good exercise.</p> <p>I used the Nifty_generator to generate a user, which just gives email, password etc. I have extended it as follows…</p> <pre><code>class AddProfileDataToUsers &lt; 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 </code></pre> <p>Here is the code to create the Properties table</p> <pre><code>class CreateProperties &lt; 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 </code></pre> <p>I added the following to the user model that was generated by the nifty_authentication generator</p> <pre><code>class User &lt; ActiveRecord::Base #other stuff in the user model up here...... validates_length_of :password, :minimum =&gt; 4, :allow_blank =&gt; true #this is the stuff that I have added to the user model has_many :managed_properties, :class_name =&gt; "Property", :foreign_key =&gt; "manager_id" has_many :owned_properties, :class_name =&gt; "Property", :foreign_key =&gt; "owner_id" has_one :rented_property, :class_name =&gt; "Property", :foreign_key =&gt; "tenant_id" </code></pre> <p>I then added this to the property model....</p> <pre><code>class Property &lt; ActiveRecord::Base belongs_to :manager, :class_name =&gt; "User" #picked up by the manager_id belongs_to :owner, :class_name =&gt; "User" #picked up by the owner_id belongs_to :tenant, :class_name =&gt; "User" #picked up by the tenant_id end </code></pre> <p>My question is, does this look like an acceptable way of modeling the situation I described?</p> <p>Should I be using the single table inheritance and creating a tenant model; a manager model; and an owner model? The problem I saw with doing that was that a single user could be both a manager and an owner. This could be solved by then having a roles tables for the user where a user has many roles and a role has many users. I had also looked at a profile table with a one to one match with the user table and making this polymorphic but I didn't think that this situation really calls for that and it didn't solve the issue where a user can be an owner and a manager.....</p> <p>This is when I started to think that maybe I was over thinking the problem and came up with what you see here. </p> <p>I welcome any constructive comments you may have. Please keep in mind that I have never actually built anything in Rails and this is all a first attempt, one week ago I had never even installed rails on my computer.</p> <p>I don't know if this matters but I figured that the admin / manager would be responsible for creating users. This will not be a self sign up type of site. The manager will add new owners when he signs up new owner, and the same will go for tenants. This will make it easier to determine the type of user he is creating.</p> <p>Thanks for any insight you may have.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload