Displaying 20 results from an estimated 84 matches for "validates_associ".
2006 Mar 13
3
validates_associated problem
Hello,
I have a situation where an ''employee'' belongs to a ''department'' and
have setup the relationship as follows.
class Employee < ActiveRecord::Base
belongs_to :department, :foregin_key => "department_id"
validates_associated :department_id
end
class Department < ActiveRecord::Base
has_many :employees
def validate_on_update
unless self.find_by_id(id) error.add(:id, "is invalid department")
end
end
However when I try to save an employee with some test department ''578'',
ge...
2006 Jun 13
6
Dead horse: validates_associated
Regarding validates_associated...
Let''s say I have:
article belongs_to author
But for whatever reason, I want an article to also be written
anonymously and therefore not require an author. Then I have:
Article:
belongs_to :author
validates_associated :author
But I DON''T have validates_presence_of...
2006 Mar 18
1
has_many and validates_associated
...to arrive to the conclusion that
1. Validation recurses through has_many by default and
a root save! call raises an excepcion if some child no
matter how nested is fails validation
2. Validation does not recurse through has_one by default
and you can change this with validates_associated, in
which case exceptions behave like above
Besides not being symmetric, the documentation of
validates_associated has an example where models associated with
has_many are passed to the method, so that makes me wonder whether 1
and 2 above are actually wrong.
-- fxn
2008 Jun 29
2
How to write test about validates_associated
...vouchertypes, :dependent => :destroy
has_one :coursecodeformat, :dependent => :destroy
validates_uniqueness_of :name_native
validates_uniqueness_of :code
validates_presence_of :code
validates_presence_of :name_native
validates_presence_of :email
validates_presence_of :phone
validates_associated :currency
validates_associated :coursecodeformat
validates_associated :vouchertypes
end
and here is my test:
require File.dirname(__FILE__) + ''/../test_helper''
class AccountTest < Test::Unit::TestCase
def test_validate_present
account = Account.new
assert...
2006 Jun 12
0
validates_associated -- explicitly setting collection?
Hi, I have a class (say Parent) that has many of two other classes, say
FirstChild and SecondChild. These could look like this:
class Parent < ActiveRecord::Base
has_many :first_children, :dependent => true
has_many :first_children, :dependent => true
validates_associated :first_children
validates_associated :second_children
end
class FirstChild < ActiveRecord::Base
belongs_to :parent
validates_format_of :birth_date, :with => /^\d{4}\/\d{2}\/\d{2}$/
end
class SecondChild < ActiveRecord::Base
belongs_to :parent
validates_format_of :birth_date...
2005 Oct 26
2
validates_associated ... doesn't
...ed (saved/created/updated) only valid club_id''s are accepted.
I have a Club model (simplified) like this:
class Club < ActiveRecord::Base
has_many :members
end
I have a Member model (simplified) like this:
class Member < ActiveRecord::Base
belongs_to :club
validates_associated :club
validates_presence_of :club_id
end
All other validations (not shown) and basic CRUD works as expected.
However, when I insert a junk club_id it is accepted and saved in the
database.
Am I doing something wrong? I am using rails release 0.13.1-1 on Ubuntu
Linux.
Thx,
Fraser
2008 Jul 01
6
validates_associated & foreign keys
Hi,
I''m struggling to get the validates_associated to work as I think it
should be.
I''m using:
JRuby 1.1
rails-2.0.2
activerecord-2.0.2
activerecord-jdbc-adapter-0.8.2
My tables in MySQL:
CREATE TABLE area_codes (
id INT UNSIGNED auto_increment primary key
...
);
CREATE TABLE markets (
id INT UNSIGNED auto_...
2013 Feb 13
3
conditional validates_associated
Hello,
Have you guys noticed that conditional validation with validates_associated
does not work well when you are creating a new record?
Consider this gist: https://gist.github.com/aflag/4780225
The Lawyer class has validates_associated on address conditioned on whether
the Lawyer data comes from a known source or not. So, if lawyer.source
equals to some string, then l...
2006 Apr 26
1
Bug in validates_associated?
Here is the setup:
Windows XP, InstantRails, MySQL 14.7, Ruby 1.8.4, Rails 1.1.2
MODEL:
class Office<ActiveRecord::Base
has_many :users
end
class User<ActiveRecord::Base
belongs_to :office
validates_presence_of :office_id
validates_associated :office
<....other stuff...>
end
UNIT_TEST test/unit/user_test.rb
def test_associations
u = User.new
u.office_id = 2222 # Invalid office id
assert !u.save # Expected because of the invalid office id
u.office_id = 2 # Valid office ID, found in fixtures
a...
2005 Nov 15
0
validates_associated
...--------------
person_id
alt_person_id
So my ActiveRecord looks like:
class PeopleAltPerson < ActiveRecord::Base
belongs_to :person
belongs_to :altperson, :class_name => ''Person'', :foreign_key =>
''alt_person_id''
validates_presence_of :person
validates_associated :person
validates_presence_of :altperson # HERE?
validates_associated :altperson # HERE?
end
It''s the last two lines I''m asking about. Can the validates_associated
macro be used in this situation? i.e. where a table has two references
to the same table....
2007 Feb 25
0
validates_associated causing weird results
I''d almost think the presence of "validates_associated" is causing the
associated model to run validation twice. The associated model, Upload,
which handles uploaded files and saves them, is saving each uploaded
file twice in a row. This doesn''t happen if I remove
"validates_associated" from the model it belongs to, or if...
2008 Mar 11
1
add_multiple_associated_save_callbacks and validates_associated conflict?
...s I''m misunderstanding what''s happening here, but I''m having a
problem with not being able to disable the validation of an associated
collection if that collection contains a new record. Example...
class Person < ActiveRecord::Base
has_many :email_addresses
validates_associated :email_addresses, :if => Proc.new { false }
end
p = Person.new
p.email_addresses.build
p.save (will return false and give you a validation error on
email_addresses)
There''s a validation set up by add_multiple_associated_save_callbacks
in ActiveRecord::Associations::ClassMethods th...
2006 Oct 14
2
issues with validates_associated not throwing error
...not throwing an error
where it should.
I have a class ''clientpool'' that is:
class Clientpool < ActiveRecord::Base
set_table_name "clientpool"
set_primary_key "id"
belongs_to :clients, :foreign_key => "cliname"
validates_presence_of :cliname
validates_associated :client
end
However, when I try to make a new clientpool, that has an invalid
cliname, that is not listed in clients, it will still create it and not
error out.
I see in the output log it does run the SQL to check, and the SQL
querry returns 0 rows, so it should error out, but it doesn'...
2006 Aug 19
1
Re: validates_associated
On 8/19/06, Alastair Moore <alastair-mb+lb5rSoAr10XsdtD+oqA@public.gmane.org> wrote:
>
> Hello all,
>
> Having a bit of a problem with validates_associated. I assume I''m missing
> something out but basically got these two models -
>
> class Seller < User
>
> has_one :profile, :foreign_key => ''user_id'', :dependent => :destroy
> has_many :properties
>
> validates_associated :profile
&...
2007 Mar 30
0
Rolling up validates_associated error messages into parent
All,
I really dig using validates_associated on my models to automatically
validate their associated objects.
However, I find that I would prefer that the associated object''s error
messages were propagated up to the original (object on which validate
was called) object as they are, instead of the original object showing
"X...
2008 Jun 10
2
validates_associated doesn't work on update (and i've tried :on => :update)
...edit/update. First the
models:
class Story < ActiveRecord::Base
attr_accessor :tag_string_holder #holds a CSV of tags
...
has_and_belongs_to_many :tags, :uniq=> true
...
def validate_associated_records_for_tags() end #to remove the
generic "is invalid" error for tags
...
validates_associated :tags, :message => ''may consist of only
letters, numbers, and underscores''
...
class Tag < ActiveRecord::Base
has_and_belongs_to_many :stories, :uniq => true, :order =>
"stories.created_at DESC"
...
validates_presence_of :tag
validates_format_of :...
2009 Oct 27
5
Unwanted call to validates_associated
...d, then i get
"Answers is not valid"
coming out of errors.full_messages. I don''t want this, i''m already
testing the validity of the answers and this is an ugly and
uninformative error message. It looks like it''s the message i would get
back if i was calling
validates_associated :answers
but i''m not, and if i *do* add this line
validates_associated :answers, :message => "are not all valid"
then i get "Answers are not all valid" AND "Answers is not valid". So,
i guess it''s coming from somewhere else. But i can...
2005 Oct 12
4
Validating 2 related models at once not working
Hello,
I have a form that submits data to two related models/tables. One is
Customer the other is Address. Validation for Customer works fine but not
for the Address.
Class Customer < ActiveRecord::Base
has_one :address
validates_presence_of :login, :password, :email
validates_associated :address
end
Class Address < ActiveRecord::Base
belongs_to :customer
validates_presence_of :address1, :city, :state, :zip
end
With the code below the validation simply doesn''t work and the address
object doesn''t get saved if all the required fields are empty.
def s...
2006 Aug 17
2
validates_associated bug?
...object itself throws no validation errors, and b) if i
change @credit_card.save to
@credit_card.update_attributes(params[:credit_card]) i get a new
credit_card record in the database, yet the overall transaction fails
later because @organization still thinks the credit card is invalid.
taking the validates_associated :credit_card out of Organization lets
the whole operation complete normally, using the same input data.
i have a model structure roughly like so:
User
id, etc...
BusinessUser
id, etc...
user_id
Organization
id, etc...
primary_business_user_id
credit_card_id
CreditCard
id, etc...
2006 Mar 14
0
validates_associated stops after first fail
Hi,
I am using validates_associated to validate some child models. The
validation of the children stops after the first failed validation. i
would like to validate all the children so that errors for more than
one can be displayed in a form. Is there a way to do this without
looping through all the children?
Thanks,
Peter