Using before_validation_on_create instead of before_create might get
you where you want to go. Depending on how you actually set the year
attribute, you might need to need to add some additional logic to
#set_year since it would be running in an object that hasn't yet been
validated.

-John

On Fri, Jan 9, 2009 at 6:03 PM, Glenn Little <[email protected]> wrote:
>
> I have an issue where I'm trying to do some validation, but part of
> the validation calculation is based on a value that I want to set
> in a before_create filter.  Unfortunately, before_create is called
> *after* the validation callbacks, so my value isn't available at
> validation time.
>
> Specifically:
>
> #-------------------------------------------------
>  validates_uniqueness_of  :account_name,
>                           :scope => :year
>
>  before_create            :set_year
>
> (etc etc etc...)
>
> protected
>  def set_year
>    self.year = <whatever is appropriate>
>  end
> #-------------------------------------------------
>
> I want to allow account names to repeat, but not within years.
>
> Is there a clean way to get this behavior without having to mess
> with the constructor?  Messing with the constructor just for this
> when it's called so often in other situations seems ham-fisted.
>
> Thanks much!
>
>        -glenn
>
> >
>



-- 
John Parker
[email protected]

--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---

Reply via email to