Hi, In my user model I validates_uniqueness_of :login. In my app, users can edit their information. If the user change the name but not the login, the record gets rejected. Of course the login is already used, but by the same user that it is editing his information. The record should not be rejected right ? Could someone tell me if it is normal that it gets rejected ? Thanks, Mickael. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
So, nobody thinks that validates_uniqueness_of should accept the edition of the same record ? Should I add an on create condition and validate the record "by hand" on edition ? Mickael. On Aug 15, 10:20 pm, MickTaiwan <faiv...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > In my user model I validates_uniqueness_of :login. > > In my app, users can edit their information. > If the user change the name but not the login, the record gets > rejected. > Of course the login is already used, but by the same user that it is > editing his information. > > The record should not be rejected right ? > Could someone tell me if it is normal that it gets rejected ? > > Thanks, > Mickael.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
you might need to :scope the contraint: http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000944 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mickael Faivre-macon wrote:> So, nobody thinks that validates_uniqueness_of should accept the > edition of the same record ? > Should I add an on create condition and validate the record "by hand" > on edition ? > Mickael.You could try using something like validates_uniqueness_of :login, :except => ''update'' I think that''s the way it goes, that way it won''t validate on the update action only on the login or what ever. Hope that helps. ~Jeremy -- 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-/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 -~----------~----~----~----~------~----~------~--~---
Jeremy: Okay, if it''s the only solution, I will do the update validation by hand. Thanks Jeremy. I was hoping Rails was a little bit more "intelligent" and it would detect that we are updating the same record, so it is OK to set the same value as before. Jemminger: I don''t think a scope can help me there. I want to validates the uniqueness of this field on the global scope. Thanks anyway. Mickael. On Aug 17, 12:30 am, Jeremy Woertink <rails-mailing-l...@andreas- s.net> wrote:> Mickael Faivre-macon wrote: > > So, nobody thinks that validates_uniqueness_of should accept the > > edition of the same record ? > > Should I add an on create condition and validate the record "by hand" > > on edition ? > > Mickael. > > You could try using something like validates_uniqueness_of :login, > :except => ''update'' > > I think that''s the way it goes, that way it won''t validate on the update > action only on the login or what ever. Hope that helps. > > ~Jeremy > -- > Posted viahttp://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-/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 -~----------~----~----~----~------~----~------~--~---
I agree that it should be more intelligent. I would think that :except => ''update'' would lead to inconsistencies unless you added a before update filter that checked this on your own...I think a select count where id != self.id and attrib = attrib would do it. I may be stating the obvious here, but I was quite surprised myself to learn that it behaves this way. MickTaiwan wrote:> Jeremy: > Okay, if it''s the only solution, I will do the update validation by > hand. Thanks Jeremy. > I was hoping Rails was a little bit more "intelligent" and it would > detect that we are updating the same record, so it is OK to set the > same value as before. > > Jemminger: > I don''t think a scope can help me there. I want to validates the > uniqueness of this field on the global scope. > Thanks anyway. > > Mickael. > > > On Aug 17, 12:30 am, Jeremy Woertink <rails-mailing-l...@andreas- > s.net> wrote: > >> Mickael Faivre-macon wrote: >> >>> So, nobody thinks that validates_uniqueness_of should accept the >>> edition of the same record ? >>> Should I add an on create condition and validate the record "by hand" >>> on edition ? >>> Mickael. >>> >> You could try using something like validates_uniqueness_of :login, >> :except => ''update'' >> >> I think that''s the way it goes, that way it won''t validate on the update >> action only on the login or what ever. Hope that helps. >> >> ~Jeremy >> -- >> Posted viahttp://www.ruby-forum.com/. >> > > > > >-- Sincerely, William Pratt http://www.billpratt.net billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
According to the source, this problem should not happen: 547: unless record.new_record? 548: condition_sql << " AND #{record.class.table_name}.#{record.class.primary_key} <> ?" 549: condition_params << record.send(:id) 550: end I''ll dig in to it next week and see whats happening. William Pratt wrote:> I agree that it should be more intelligent. I would think that :except > => ''update'' would lead to inconsistencies unless you added a before > update filter that checked this on your own...I think a select count > where id != self.id and attrib = attrib would do it. I may be stating > the obvious here, but I was quite surprised myself to learn that it > behaves this way. > > > MickTaiwan wrote: >> Jeremy: >> Okay, if it''s the only solution, I will do the update validation by >> hand. Thanks Jeremy. >> I was hoping Rails was a little bit more "intelligent" and it would >> detect that we are updating the same record, so it is OK to set the >> same value as before. >> >> Jemminger: >> I don''t think a scope can help me there. I want to validates the >> uniqueness of this field on the global scope. >> Thanks anyway. >> >> Mickael. >> >> >> On Aug 17, 12:30 am, Jeremy Woertink <rails-mailing-l...@andreas- >> s.net> wrote: >> >>> Mickael Faivre-macon wrote: >>> >>>> So, nobody thinks that validates_uniqueness_of should accept the >>>> edition of the same record ? >>>> Should I add an on create condition and validate the record "by hand" >>>> on edition ? >>>> Mickael. >>>> >>> You could try using something like validates_uniqueness_of :login, >>> :except => ''update'' >>> >>> I think that''s the way it goes, that way it won''t validate on the update >>> action only on the login or what ever. Hope that helps. >>> >>> ~Jeremy >>> -- >>> Posted viahttp://www.ruby-forum.com/. >>> >> >> >> >> >> > > -- > Sincerely, > > William Pratt > > http://www.billpratt.net > billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org-- Sincerely, William Pratt http://www.billpratt.net billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I played around with this a little, and I was mistaking yesterday. I am seeing the correct behavior on updates with validates_uniqueness_of. If the duplicate is in the record I am updating, the validation passes. in my asset.rb model: ... validates_uniqueness_of :asset_tag, :scope => :company_id ... in irb: >> a = Asset.find(:first) ... >> a.asset_tag => "AS00003" >> a.new_record? => nil >> a.asset_tag = "AS00003" => "AS00003" >> a.new_record? => nil >> a.valid? => true William Pratt wrote:> According to the source, this problem should not happen: > > 547: unless record.new_record? > 548: condition_sql << " AND #{record.class.table_name}.#{record.class.primary_key} <> ?" > 549: condition_params << record.send(:id) > 550: end > I''ll dig in to it next week and see whats happening. > > William Pratt wrote: >> I agree that it should be more intelligent. I would think that >> :except => ''update'' would lead to inconsistencies unless you added a >> before update filter that checked this on your own...I think a select >> count where id != self.id and attrib = attrib would do it. I may be >> stating the obvious here, but I was quite surprised myself to learn >> that it behaves this way. >> >> >> MickTaiwan wrote: >>> Jeremy: >>> Okay, if it''s the only solution, I will do the update validation by >>> hand. Thanks Jeremy. >>> I was hoping Rails was a little bit more "intelligent" and it would >>> detect that we are updating the same record, so it is OK to set the >>> same value as before. >>> >>> Jemminger: >>> I don''t think a scope can help me there. I want to validates the >>> uniqueness of this field on the global scope. >>> Thanks anyway. >>> >>> Mickael. >>> >>> >>> On Aug 17, 12:30 am, Jeremy Woertink <rails-mailing-l...@andreas- >>> s.net> wrote: >>> >>>> Mickael Faivre-macon wrote: >>>> >>>>> So, nobody thinks that validates_uniqueness_of should accept the >>>>> edition of the same record ? >>>>> Should I add an on create condition and validate the record "by hand" >>>>> on edition ? >>>>> Mickael. >>>>> >>>> You could try using something like validates_uniqueness_of :login, >>>> :except => ''update'' >>>> >>>> I think that''s the way it goes, that way it won''t validate on the update >>>> action only on the login or what ever. Hope that helps. >>>> >>>> ~Jeremy >>>> -- >>>> Posted viahttp://www.ruby-forum.com/. >>>> >>> >>> >>> >>> >> >> -- >> Sincerely, >> >> William Pratt >> >> http://www.billpratt.net >> billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org > > -- > Sincerely, > > William Pratt > > http://www.billpratt.net > billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org > > >-- Sincerely, William Pratt http://www.billpratt.net billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for the update. I had implemented a solution "by hand", with an :on=>:create and doing the validation by hand on update. After seeing the source you just sent, I reverted to the old (and to me "normal") way of doing it, and then it worked. I don''t know why it failed earlier. Sorry for the fuss, and again thank you for the work you''ve done. Mickael. On 8/18/07, William Pratt <billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> > I played around with this a little, and I was mistaking yesterday. I am > seeing the correct behavior on updates with validates_uniqueness_of. If the > duplicate is in the record I am updating, the validation passes. > > in my asset.rb model: > ... > validates_uniqueness_of :asset_tag, :scope => :company_id > ... > > in irb: > >> a = Asset.find(:first) > ... > >> a.asset_tag > => "AS00003" > >> a.new_record? > => nil > >> a.asset_tag = "AS00003" > => "AS00003" > >> a.new_record? > => nil > >> a.valid? > => true > > > > William Pratt wrote: > According to the source, this problem should not happen: > > 547: unless record.new_record? > 548: condition_sql << " AND > #{record.class.table_name}.#{record.class.primary_key} <> ?" > 549: condition_params << record.send(:id) > 550: end > I''ll dig in to it next week and see whats happening. > > William Pratt wrote: > I agree that it should be more intelligent. I would think that :except => > ''update'' would lead to inconsistencies unless you added a before update > filter that checked this on your own...I think a select count where id !> self.id and attrib = attrib would do it. I may be stating the obvious here, > but I was quite surprised myself to learn that it behaves this way. > > > MickTaiwan wrote: > Jeremy: > Okay, if it''s the only solution, I will do the update validation by > hand. Thanks Jeremy. > I was hoping Rails was a little bit more "intelligent" and it would > detect that we are updating the same record, so it is OK to set the > same value as before. > > Jemminger: > I don''t think a scope can help me there. I want to validates the > uniqueness of this field on the global scope. > Thanks anyway. > > Mickael. > > > On Aug 17, 12:30 am, Jeremy Woertink <rails-mailing-l...@andreas- > s.net> wrote: > > > Mickael Faivre-macon wrote: > > > So, nobody thinks that validates_uniqueness_of should accept the > edition of the same record ? > Should I add an on create condition and validate the record "by hand" > on edition ? > Mickael. > > You could try using something like validates_uniqueness_of :login, > :except => ''update'' > > I think that''s the way it goes, that way it won''t validate on the update > action only on the login or what ever. Hope that helps. > > ~Jeremy > -- > Posted viahttp://www.ruby-forum.com/. > > > > > > -- > Sincerely, > > William Pratt > > http://www.billpratt.net > billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org > > -- > Sincerely, > > William Pratt > > http://www.billpratt.net > billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org > > > > -- > Sincerely, > > William Pratt > > http://www.billpratt.net > billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org > > > >-- Mickael. Coding an AI ! http://faivrem.googlepages.com/antbattle --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---