Has_one with conditions

I have 2 model which look like this:

class Shift<ActiveRecord::Base
belongs_to :person

class Person<ActiveRecord::Base
has_many :shifts
has_one :open_shift, :classname => ‘Shift’, :conditions => [‘open =
?’, true], :order => ‘id desc’

So a person works many shifts, and may have a shift which is currently
active, which i’ve called an open_shift.
I’ve noticed the following behaviour, which seems a little to me. Assume
bob is an instance of person, and that bob has an open_shift

If I do:
open = bob.open_shift
open_2 = Shift.find(open.id)
open_2.open = false
Then open will be nil, I wouldn’t have expected that at all. if i do
open2.reload then open_2 is not nilled out. It’s as if open remembers
where it comes from and is actually do the full find when I call reload,
rather than just reloading the Shift with the appropriate id.

Can anyone shed some light on this?


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs