Note that there are some explanatory texts on larger screens.

plurals
  1. POOne-to-one association in Rails
    primarykey
    data
    text
    <p>My bussiness logic is as follows: I have a number of users. Each user can be either a requester or a worker.</p> <p>This is how I implemented it in Rails. I'm using devise for authentication.</p> <pre><code>class User &lt; 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 </code></pre> <p>Here is the requester model.</p> <pre><code>class Requester &lt; ActiveRecord::Base attr_accessible :rating belongs_to :user has_many :tasks end </code></pre> <p>When I tried to test it with cucumber, I defined a method to create a number of users</p> <pre><code>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 </code></pre> <p>And create requester from user</p> <pre><code>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 </code></pre> <p>When I debugged with pry, I found that <code>requester.user</code> returned a user but <code>r = Requester.first</code> then <code>r.user</code> return nil (I checked that <code>r == requester</code> return true). That is weird. </p> <p>Speculating that the problem comes from not preparing the test database, I have run <code>rake db:test:prepare</code> to check but <code>r.user</code> still return nil.</p> <p>Another question: with this model, can I call <code>user.requester</code> to get the requester belongs to this user ?</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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