My bussiness logic is as follows: I have a number of users. Each user can be either a requester or a worker.
This is how I implemented it in Rails. I'm using devise for authentication.
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :confirmed_at
has_one :requester
end
Here is the requester model.
class Requester < ActiveRecord::Base
attr_accessible :rating
belongs_to :user
has_many :tasks
end
When I tried to test it with cucumber, I defined a method to create a number of users
def create_confirmed_user(name,email,password)
user = User.new(name: name, email: email, password: password, password_confirmation: password, confirmed_at: DateTime.now)
user.skip_confirmation!
user.save
end
And create requester from user
def create_requester_from_user(email, rating)
user = User.find_by_email(email)
requester = Requester.new(rating: rating)
requester.user = user
requester.save
binding.pry #To debug
0
end
When I debugged with pry, I found that requester.user
returned a user but r = Requester.first
then r.user
return nil (I checked that r == requester
return true). That is weird.
Speculating that the problem comes from not preparing the test database, I have run rake db:test:prepare
to check but r.user
still return nil.
Another question: with this model, can I call user.requester
to get the requester belongs to this user ?
One more question is that how to implement this bussiness logic properly ? Is Single Table Inheritance (STI) a good choice ? Since I googled and found that using STI has many bad side-effects.