I actually implemented my code this way, it uses devise (see devise docs), but 
adds these to the model:

  validate :password_not_blank

  def password_not_blank
    if !self.password.nil? && self.password.blank?
      errors.add(:password, "can not be blank")
    end
  end



this is completely counter-intuitive to read, although was the only way I could 
get around the chicken-egg problems created by the fact that password= and 
password_confirmation= are "settable" attributes on my model but the actual 
field used by devise is encrypted_password (this part comes from the devise 
gem).

If I just do validates :password, then it hiccups when you go to save a record 
anywhere in the app except upon creation (that is, anywhere where password= 
hasn't been called on the instance)

Basically it is saying if the password is NOT nil (we aren't trying to set it 
in the action), then also make sure it is not actually empty string.

This is a common problem I've seen before. Not to beat a dead horse, but does 
anyone have any other patterns to this kind of a problem?


Thanks,
Jason




On Aug 5, 2014, at 9:24 AM, Jason Fleetwood-Boldt <t...@datatravels.com> wrote:

> 
> On Aug 5, 2014, at 2:44 AM, Ronald Fischer <li...@ruby-forum.com> wrote:
> 
>> lambda {
>> t=self.password_validation_required
>> self.password_validation_required=true
>> t
>> }
>> 
>> or can this potentially lead to problems later on?
>> 
> 
> 
> I would avoid that. Without looking at your whole code I can't really say the 
> right way to do that. However, I recommend you take a look at devise. It's a 
> little top-heavy but it's worth it once you get the hang of it. Also, if you 
> don't want to actually use it, read the source code and see how they do it 
> with password validation. 
> 
> 
> 
> 
>> Finally, a syntax question: You are writing
>> 
>> lambda {  .... }
>> 
>> I would have written
>> 
>> -> { ... }
> 
> 
> You're right -- they are equivalent in Ruby 2.0 (and to make matters worse I 
> actually switched between Ruby 1 and Ruby 2 syntax in the same line of code). 
> 
> Probably best to stick with the shorthand if you're using Ruby 2.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to rubyonrails-talk+unsubscr...@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/7685EEB7-3D84-4296-9178-9AB0AA3672FB%40datatravels.com.
> For more options, visit https://groups.google.com/d/optout.
> 

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/F53CAC68-BDED-4E2C-B3FD-012CA24626B9%40datatravels.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to