Displaying 20 results from an estimated 52 matches for "before_validate".
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
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
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
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,
2009 Jun 24
2
At my wits end ! Controlling passed in negative values from a form
Hi,
I am trying to validate a numeric value passed in from a form, which
is saved in a MySQL db as decimal(9,2)
Now problem is I need to ensure people don''t enter a form value like -.
003. In my validations, I have a validates_numericality_of check being
done. Furthermore, in validate method, I have a check using an if
statement such as :
((an_object[n].nil? or an_object[n] < 0) and
2006 Mar 25
0
[Sentry]-before_validation(model) not working for crypted-attribute?
I am still trying to get sentry working to encrypt a couple of
attributes in the model. One of them is a password field and the other
ssn#.
I have crypted_password and crypted_ssn in the db.
In addition, I also have validates_uniqueness_of :ssn
Here is the problem... I get the error
MysqlError: Unknown column ''ssn'' in ''where clause'': SELECT * FROM
students
2012 Nov 10
6
Suggestion: `before_save on: :create` should either work or raise an exception
There''s a small inconsistency in ActiveRecord''s callback 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`,
2006 Apr 27
3
Removing a default value for a foreign key with not null set
Hi all,
For various reasons I need to have foreign keys with not null constraints.
Eg:
create_table :people do |t|
t.column :name, :string
t.column :household_id, :integer, :null => false
end
create_table :households do |t|
t.column :name, :string
end
add_foreign_key_constraint :people, :household_id, :households, :id
So the people table has a foreign key into
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
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?
Buildings have units. Units have rooms. Rooms 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
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.
[] (obviously from the url[0..3] bit)
It works
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
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
2005 Apr 04
1
Validation problems: ActiveRecord::AssociationTypeMismatch
Hi everyone, I''m starting on my first rails project and I have a
problem that I''m sure has a simple solution. I have two models, an
address model and a state model. The address belongs_to :state,
foreign_key=>"state" and validate_associated :state
I have new & edit controllers for address which contain a drop down
list for the state choice. My list was
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