How about this ? validate do |applicant| errors.add_to_base("Things must check out!") unless applicant.check_things end
def before_save applicant.non_represented_field = 123456789 end before_save will only be called if your validations pass, so non_represented_field will only be assigned to valid records. Luke On 2010-10-05, at 1:46 PM, Jim Burgess wrote: > Hi, > > I have a web-based form for applicants to enter their details, created > using form_for :applicant > > There is one field in the corresponding applicants table of my database > which is not represented in the form. > > I want to set the value of this field in the model (it should be an > unique hash). > > What is the best way to do this? > > Currently I am doing: > > validate do |applicant| > applicant.check_things > applicant.non_represented_field = 123456789 > end > > This works, but the value assigned to the 'non-represented' field is > only available after the applicants details have been successfully > saved. > > If validation fails (i.e. if an obligatory field was omitted), this > value is showing up in the hash of values that is posted to the > controller as nil (which is bad as I always want it to be set). > > I would be grateful if anyone could give me an idea how to solve this. > The only other thing I could think of was setting it with some kind of > call back, but I couldn't get that to work. > > Thanks in advance. > -- > 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-t...@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > -- 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-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.