On Thu, Nov 4, 2010 at 6:30 AM, Gabriel Saravia <[email protected]>wrote:

> So, I'm attempting to make sure that the associated user_id of an object
> cannot be changed on update and I'm doing this with a before_update
> callback and this method in the model:
>
>    before_update :lock_user_id
>
>    def lock_user_id
>      if self.user_id_changed?
>        self.user_id = self.user_id_was unless self.user_id_was == nil
>      end
>      true
>    end
>
> but this spec fails:
>
>  it "user_id cannot be changed once QR has been created" do
>    @qr.update_attributes(:user_id => -1)
>    @qr.user_id.should == @user.id
>  end
>
> at first I thought it was that update_attributes did not do a complete
> save (not that i've ever enabled partial saves), but this alternate spec
> also fails:
>
>  it "user_id cannot be changed once QR has been created" do
>    @qr.user_id = 99
>    @qr.save
>    @qr.user_id.should == @user.id
>  end
>
> even stranger is that a method in another model which does the same
> thing for another attribute passes its test.
>
> Anybody have any idea what i'm doing wrong/alternate suggestions?
>
> Thanks.


Only thing I could think of is validations for QR did not pass, which means
that the before_update callback doesn't get to be called at all.

-- 
Erol M. Fornoles
http://github.com/Erol
http://twitter.com/erolfornoles
http://ph.linkedin.com/in/erolfornoles

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to