search for: before_validation

Displaying 20 results from an estimated 52 matches for "before_validation".

2007 Apr 05
0
unexpected behavior of before_validation
Today I found these comments in active record''s callbacks.rb file: # If the returning value of a before_validation callback can be evaluated to false, the process will be aborted and Base#save will return false. # If Base#save! is called it will raise a RecordNotSave error. # Nothing will be appended to the errors object. This caused me a lot of frustration earlier when I was trying to get a model with the...
2006 Mar 24
1
Problems with before_validation
Hi all My visitors can create party organisator profiles, and I want assign them as the creators (the field creator_id in the organisators table references a member in the members table). To do that I tried it with before_validation: def before_validation creator = Member.find 1 end Sadly, this does not work. The validation validates_presence_of :creator_id Still tells me, that creator needs a value. What''s wrong there? Somehow the attribute gets not filled with a value... I tried ith with self.creator...
2011 Jun 19
1
before before_validation callback
Hi, I was looking around a bit and couldn''t find any callbacks that executed before before_validation The current problem with using before_validation is that it won''t fire if I''m not using validations with #save(:validate => false) - which makes sense. Before_save won''t suffice because I want these callbacks to fire before validation on the occasions that I do use va...
2012 May 26
2
before_create is after_validatation :on => :create , when it should be before_validation :on => :create
In ActiveRecord''s callbacks, methods within a before_create callback are called after the validation, but before the model is created. This was an intentional design decision, but I think it was the wrong one. What do you think? When I think of before_create, I think of before_validation :foo, :on => :create, but, by default, before_create is synonymous with after_validation :foo, :on => :create`, which is technically correct, but confusing. Most of the goal of convention over configuration is to have sane defaults, right? Wouldn''t following that principle include fa...
2009 Jun 24
2
At my wits end ! Controlling passed in negative values from a form
...n can catch -0.003. However, it just does not catch -.003 (without leading zero). In fact, in case of -. 003, the value gets saved as 0; if form value is -.008 let''s say, it gets saved as .01 in db (gets rounded off). Anything like -1.23 works ok - meaning it is trapped as error. I have a before_validation method which assigns 0 to undefined values like so: self.xxx = 0 unless self.xxx (where xxx is name of attribute) I have tried checking for a minus sign in before_validation & validate method using xxx.to_s.include?("-"). I have also tried using :greater_than and :minimum options in...
2006 Mar 25
0
[Sentry]-before_validation(model) not working for crypted-attribute?
...ect_all'' .../active_record/base.rb:431:in `find_by_sql'' .../active_record/base.rb:395:in `find'' .../activerecord-1.13.2/lib/active_record/base.rb:393:in `find'' .../activerecord-1.13.2/lib/active_record/validations.rb:502:in `validates_uniqueness_of'' THe before_validation code in sentry is not substituting crypted_ssn for ssn and also is not providing the encrypted value to the validation code so that the table can be searched for uniqueness. Can some one (please Rick :) shed some light on this for me? THank you very much. -bakki
2012 Nov 10
6
Suggestion: `before_save on: :create` should either work or raise an exception
...allback syntax that has tripped me up before. It wouldn''t be a big deal, but it can lead to a silent failure. I''d like to suggest that it either be made consistent or be made to fail loudly. The issue is that to do something before validating, but only when creating, you use `before_validation on: :create`, but to do something before saving, but only when creating, you use `before_create`. I have tried to use `before_save on: :create`, and it does not produce an error, but it does something unexpected: it saves before create **and** before update. This isn''t really somethin...
2006 Apr 27
3
Removing a default value for a foreign key with not null set
...mn. My problem is that I get default values of 0 for household_id when instantiating new Person object. Person.new.household_id # == 0 validates_presence_of :household_id then passes because of the 0 value, and the insert fails because 0 is not a valid foreign key. A simple fix is to use a before_validation and remove and 0 values for _id fields, but I''m looking for a cleaner solution: class Person < ActiveRecord::Base belongs_to :household validates_presence_of :household_id def before_validation attributes.each do |key, value| self[key] = nil if value == 0 and key.ends_...
2006 Apr 05
4
validates_format_of ?
Hi all, I''ve got a form which accepts a "price" field and I''m attempting to validate it and allow some flexibility in the way the user enters it. It''s a numeric field but I want the user to be able to enter dollar signs and commas naturally in the data entry. I''m trying to understand how to use validates_format_of, and I''m not sure what
2006 May 02
4
useful bit of code (hopefully)
Hi, I often find myself using bits of code like this inside ActiveRecord, perhaps it''s useful for others, or others can improve on it: ########### # fix user input before validating it before_validation :sanitize_input # santize input before actual validation is called # this uses the little methods defined below def sanitize_input trim %w(adres postcode woonplaats email naam telefoon mobiel) downcase :email empty_to_nil %w(telefoon mobiel) end # pass in a list of fields whose val...
2005 Dec 20
2
Null Text Fields
I tend to allow nulls in cases where the value is unknown, even if it''s a text/varchar field. RoR seems to push empty strings into these fields. Is there any way to get RoR to use NULL vice '''' if the input box is empty? Thanks! - Ian
2006 Nov 10
3
Move variables from session to ActiveRecord?
...oms have people. I can grab building, room, and people IDs and store them in a session[:variable]. However, when creating a new unit, I need to give that session[:building_id] to ActiveRecord for creation. I''m not sure whether to use hidden fields or do it all in the model ''before_validation'' - in either case I don''t know how. My attempt failed with an ''Invalid method error'': before_validation :set_building_id def set_building_id @unit.building_id = session[:building_id] end How can I add session variables to insert/create statements?...
2010 Jun 25
3
Best practice for removing leading and trailing whitespaces
I want to remove the leading and training spaces for most of the resources in my rails application For a new POST request, rails does the xml parsing and passes the params hash to the controller. Some of the parameters have leading and/or trailing whitespaces which are causing problems (e.g. new resources being created rather than using the old ones). I don''t want to put the code in all
2008 Jan 14
2
simple syntax question (models, validation)
who can explain this to me? def before_validation logger.info url url = "http://" << url if url[0..3]=="www." end the logger line correctly logs any URL that''s been submitted, let''s say it''s "www.amazon.co.uk" BUT Rails raises an exception on the next line, complaining about nil...
2006 May 09
11
model filter?
Hi All, Is there a way to filter / modify the output of a model class before it''s returned to controller/view that''s calling it? Example: I have a field phone_number which I always want to preformat using the number_to_phone() helper.... Any help is appriciated. Thanks! -- Posted via http://www.ruby-forum.com/.
2006 Jun 15
1
callback object problem
I''m trying to create a separate class to do validation so that about 10 models can use the same date validation. I have tried to use the example from the agile book but it''s not working. Here''s what I have: app/models/date_callbacks.rb class DateCallbacks def before_validation(model) if model.start_date > Date.today model.errors.add(:start_date, "must be before today") end if !model.end_date.nil? if model.end_date > Date.today model.errors.add(:end_date, "must be before today") end end if !model.end_date.nil? if model...
2006 Feb 13
0
Including a Module within a model
If I have a module with a callback in it: module Sanitize module PhoneNumber def before_validation self.phone.gsub!(/\D/, '''') if attribute_present?(''phone'') end end module MobileNumber def before_validation self.mobile.gsub!(/\D/, '''') if attribute_present?(''mobile'') end end end and the...
2005 Apr 04
1
Validation problems: ActiveRecord::AssociationTypeMismatch
...y new record (or edit an existing one) I get an ActiveRecord::AssociationTypeMismatch Expected type State but found string instead. I can see that the proper ID is being returned, it''s just being read by ruby as a string and I''m not sure how to cast it. I tried using the various before_validation functions, but they never seem to be called (I was explicitly throwing exceptions inside them to make sure). I''m totally stuck. --Pat
2007 May 28
2
out-cheat validates_uniqueness_of
before_save :strip_strings validates_uniqueness_of :username validates_presence_of :username private def strip_strings self.username = username.gsub!(" ","") end example: username = " gissmoh-/upiLifMog2ELgA04lAiVw@public.gmane.org " becomes gissmoh-/upiLifMog2ELgA04lAiVw@public.gmane.org and is stored in the
2008 Apr 17
1
Callback Problem: Why to use self.var= instead of @var=
Hi, I''ve defined the following model: class Eintrag < ActiveRecord::Base before_validation :vali protected def vali # self.feld1.reverse! self.feld2 = self.feld1 end end Now I''m wondering why I have to write self.feld2=self.feld1 and why @feld2=@feld1 doesn''t work. I''ve found the thread http://groups.google.com/group/rubyonrails-talk/browse_thr...