julien.guimont-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-26 23:51 UTC
AR Callbacks and associations
Hello,
I have this model in Rails 2.0:
class Contact < ActiveRecord::Base
before_save :update_contact_user
belongs_to :user
belongs_to :contact_user, :class_name=>''User'',
:foreign_key=>''contact_user_id''
validates_presence_of :user, :fullname, :email
validates_as_email :email
validate_phone :home_tel, :mobile_tel, :office_tel
private
def update_contact_user
#check if some user matches the email entered
cuser = User.find :first, :conditions=>["email = ?", email]
contact_user = cuser if cuser
end
end
The callback is called correctly and finds a user, but for some reason
the association is not updated. If I do the same code in the calling
method (just before I call ''save'') then it is working. Any
guess? Am I
missing something?
Thank you,
Julien.
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Julien :> I have this model in Rails 2.0: > class Contact < ActiveRecord::Base > before_save :update_contact_user > > belongs_to :user > > belongs_to :contact_user, :class_name=>''User'', :foreign_key=>''contact_user_id'' > > validates_presence_of :user, :fullname, :email > validates_as_email :email > validate_phone :home_tel, :mobile_tel, :office_tel > > private > def update_contact_user > #check if some user matches the email entered > cuser = User.find :first, :conditions=>["email = ?", email] > contact_user = cuser if cuser > end > > end > > The callback is called correctly and finds a user, but for some reason > the association is not updated. If I do the same code in the calling > method (just before I call ''save'') then it is working. Any guess? Am I > missing something?in update_contact_user method, contact_user is a local variable. Try : self.contact_user = cuser if cuser -- Jean-François. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
julien.guimont-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-27 02:27 UTC
Re: AR Callbacks and associations
Duh. Guess you should not code after Christmas :) Thanks, Julien. On Dec 26, 8:40 pm, "Jean-François Trân" <jft...-HujFcYLiWL6M4zKIHC2jIg@public.gmane.org> wrote:> Julien : > > > > > I have this model in Rails 2.0: > > class Contact < ActiveRecord::Base > > before_save :update_contact_user > > > belongs_to :user > > > belongs_to :contact_user, :class_name=>''User'', :foreign_key=>''contact_user_id'' > > > validates_presence_of :user, :fullname, :email > > validates_as_email :email > > validate_phone :home_tel, :mobile_tel, :office_tel > > > private > > def update_contact_user > > #check if some user matches the email entered > > cuser = User.find :first, :conditions=>["email = ?", email] > > contact_user = cuser if cuser > > end > > > end > > > The callback is called correctly and finds a user, but for some reason > > the association is not updated. If I do the same code in the calling > > method (just before I call ''save'') then it is working. Any guess? Am I > > missing something? > > in update_contact_user method, contact_user is a local variable. > > Try : self.contact_user = cuser if cuser > > -- Jean-François.--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---