I\'m trying to make a RoR application for a Hospital so it has patients, doctors, offices, etc.
The problem I\'m having is that, at the patient \"Sign-up\", I\'m not
So in your controller your params are nil but you call .to_s
and .to_s.to_i
which results in an empty string "" and 0 (zero). You then save them into your database. A couple recommendations:
def pat_create
pat = Patient.new(:name => params[:name], :pid =>params[:pid])
pat.save
end
In addition to the uniqueness validation I would make sure your db column has a unique index on it to insure no duplicate patients.
class Patient < ActiveRecord::Base
attr_accessible :name, :pid
attr_accessor :name, :pid
has_many :appointments
validates :name, presence: true,
length: { maximum: 50 },
format: {:with=> /^[a-zA-Z\s\D]+$/}
validates :pid, presence: true,
numericality: { only_integer: true,
greater_than_or_equal_to: 100000,
less_than_or_equal_to: 9999999999 },
uniqueness: true
end
If you are getting valid values this will work and if not you will see why it is not.
Remove the following line in class Patient
:
attr_accessor :name, :pID
What happened was that attr_accessor replaced the two database column attributes :name
and :pID
(which were automatically generated) with its own, resulting in two virtual attributes, :name
and :pID
.
Thus, the virtual attributes were being set and validated instead of the corresponding database attributes, which resulted in no errors yet null values in the database.
Can you show us how this method is called? Also are you sure that params[:name] and params[:pid].
You have used the column :pid and :pID, as below
pat = Patient.where(:pID => id).first
if pat == nil
pat = Patient.new(:name => pName, :pID =>id) # should use pat = Patient.new(:name => pName, :pid =>id)
pat.save
end