Hi, I''m puzzzled.... I have this model: class UserCampaign < ActiveRecord::Base belongs_to :user belongs_to :campaign attr_accessible :is_active, :user_id, :campaign_id validates_presence_of :is_active, :user_id, :campaign_id end and have this observer: class UserCampaignObserver < ActiveRecord::Observer def after_create(user_campaign) update_active(user_campaign) end def after_update(user_campaign) update_active(user_campaign) end private def update_active(user_campaign) if user_campaign.is_active ucs = UserCampaign.find_all_by_user_id_and_is_active( user_campaign.user.id,true) for uc in ucs do if uc.id != user_campaign.id uc.is_active = false uc.save! end end end end end This is simple to guarantee that only one UserCampaign is active. The problem is that when the observer runs, in uc.save!, the validation fails giving me an error saying that the attribute is_active is required.... If I remove is_required attribute from the list of validates_presence_of, everything goes fine. Why it gives me that error saying that is_active is required when I have set it just before the save in the observer? Thanks, -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Just to add more information, I''m using RoR 2.3.5. Thanks On Thu, Apr 15, 2010 at 12:36 AM, Jonhy Pear <jonhy.pear-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, I''m puzzzled.... > > I have this model: > > class UserCampaign < ActiveRecord::Base > belongs_to :user > belongs_to :campaign > attr_accessible :is_active, :user_id, :campaign_id > > validates_presence_of :is_active, :user_id, :campaign_id > end > > > and have this observer: > > class UserCampaignObserver < ActiveRecord::Observer > > def after_create(user_campaign) > update_active(user_campaign) > end > > def after_update(user_campaign) > update_active(user_campaign) > end > > private > > def update_active(user_campaign) > if user_campaign.is_active > ucs = UserCampaign.find_all_by_user_id_and_is_active( > user_campaign.user.id,true) > for uc in ucs do > if uc.id != user_campaign.id > uc.is_active = false > uc.save! > end > end > end > end > > end > > This is simple to guarantee that only one UserCampaign is active. > > The problem is that when the observer runs, in uc.save!, the validation > fails giving me an error saying that the attribute is_active is > required.... > > If I remove is_required attribute from the list > of validates_presence_of, everything goes fine. > > Why it gives me that error saying that is_active is required when I have > set it just before the save in the observer? > > Thanks, >-- João Miguel Pereira http://jpereira.eu LinkedIn: http://www.linkedin.com/in/joaomiguelpereira joaomiguel.pereira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org (351) 96 275 68 58 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
validates_presence_of checks the attribute according to Object.blank? "An object is blank if it‘s false, empty, or a whitespace string. For example, "", " ", nil, [], and {} are blank." When you set a value to false it does not pass the validation. If you want to confirm that a value is either true or false you will have to it in another way. Jonhy Pear wrote:> Just to add more information, I''m using RoR 2.3.5. > > Thanks > > On Thu, Apr 15, 2010 at 12:36 AM, Jonhy Pear <jonhy.pear-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > >> end >> def after_update(user_campaign) >> if uc.id != user_campaign.id >> >> Thanks, >> > > > > -- > João Miguel Pereira > http://jpereira.eu > LinkedIn: http://www.linkedin.com/in/joaomiguelpereira > joaomiguel.pereira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > (351) 96 275 68 58-- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.