1

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.

thd
  • 2,380
  • 1
  • 25
  • 33

2 Answers2

1

I believe you want a polymorphic association http://guides.rubyonrails.org/association_basics.html

TheIrishGuy
  • 2,531
  • 19
  • 23
  • From my understanding and according to this article http://code.alexreisner.com/articles/single-table-inheritance-in-rails.html, I believe that polymorphic association and STI are different. – thd May 09 '12 at 15:44
  • 1
    STI will work you fine. Using a gem like https://github.com/brunofrank/class-table-inheritance will greatly alleviate STI caveats. – TheIrishGuy May 09 '12 at 15:51
  • What I'm afraid is that the gem is nearly one year old. However, the gem only answers one question. There are still two questions (as described above) that I can answer. – thd May 10 '12 at 02:26
  • I think the cause that requester.user returns a user and r.user returns nil is that requester is stored in memory when called but r is stored in database. – thd May 10 '12 at 02:28
0

I have figured out the problem from this post Using build with a has_one association in rails: I modified the model only without creating the migration. I need to run this migration after add belongs_to to my Requester model: rails generate migration add_user_id_to_requester user_id:integer. It's surprised that many documentations don't mention this.

Community
  • 1
  • 1
thd
  • 2,380
  • 1
  • 25
  • 33