Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5369 (reopened defect)

Opened 2 years ago

Last modified 2 months ago

[PATCH] validates_associated fails when associated id is incorrect

Reported by: kevin.olbrich@gmail.com Assigned to: David
Priority: normal Milestone: 1.2.7
Component: ActiveRecord Version:
Severity: major Keywords: validates_associated, validation
Cc: tarmoitech.ee, dkubb, JakeB, b.eggleston@gmail.com, stephen@touset.org

Description

If a model is defined wth an association like..

belongs_to :item
validates_associated :item

if a record is created that has an attribute of 'item_id' that does not correspond to a valid record, then validates_associated will not indicate an invalid association.

When the foreign key points to an invalid record, then thing.item => nil, and validates_associated can't tell if this means the foreign key is invalid or if there is no associated item.

The expected behavior would be for validates_associated to complain.

This condition can occur during testing, and if associated items are not correctly deleted.

Attachments

validates_associated.zip (18.1 kB) - added by kevin.olbrich@gmail.com on 06/13/06 20:40:24.
validates_associated plugin
ticket5369.patch (2.2 kB) - added by JakeB on 05/03/07 21:23:50.

Change History

06/13/06 02:36:38 changed by t.lucas@toolmantim.com

  • status changed from new to closed.
  • resolution set to invalid.

See the note in the validates_associated documentation:

NOTE: This validation will not fail if the association hasn’t been assigned. If you want to ensure that the association is both present and guaranteed to be valid, you also need to use validates_presence_of.

06/13/06 02:56:30 changed by kevin.olbrich@gmail.com

  • status changed from closed to reopened.
  • resolution deleted.

The documentation does not indicate that the validation will fail if the foreign key points to an invalid record number.

For example, if you create a class like...

class Item < AR:Base
  belongs_to :thing
end

if 'thing_id' is set to 1000 without a corresponding record (id=1000) in the 'things' table, the validates_associated will not indicate an invalid association.

This appears like incorrect behavior to me as it appears as if the association is assigned, but the associated object does not exist. This should respond as an invalid association.

Validates_presence_of will not detect this problem since the foreign key is not empty.

I am currently testing a plugin that corrects this behavior.

06/13/06 20:40:24 changed by kevin.olbrich@gmail.com

  • attachment validates_associated.zip added.

validates_associated plugin

12/06/06 21:07:16 changed by kian

I don't understand why this works this way.

Why is the ID not validated to be correct? Can someone please explain this?

Are we supposed to always assign the associated object instead of assigning the FK ID?

If so, what is the point of validating it? The only thing the validation will catch is if you create a new associated object instead of retrieving it from the database, and it fails to save.

Typical program flow is something like:

  • 'edit' action (or 'new')
  • possible FK objects loaded from database into instance var
  • 'edit' form displays a SELECT tag created from possible FK objects
  • data posted from form (SELECT tag posts the ID of the selected FK object)
  • 'update' action (or 'create')
  • assign params (including the FK ID) to new object or use create or something
  • save

If I have to load the associated object from the DB in my 'update' action, and then assign it to association in the object I'm updating, why bother to do validation? Heck I just retrieved it from the database, it better be valid (concurrency issues aside).

Plus, why force me to load the object from the database? I should just assign the ID (that's what I have anyway) and let Rails load it when it checks the association for validity.

12/25/06 01:41:28 changed by tarmo

  • cc set to tarmo@itech.ee.

(follow-up: ↓ 8 ) 02/21/07 23:05:12 changed by dkubb

  • cc changed from tarmo@itech.ee to tarmo@itech.ee, dkubb.

I think this should be included in core.

Would the submitter consider converting their plugin into a patch and include test cases?

02/22/07 00:02:09 changed by olbrich

yes, I'll do just that.

04/23/07 05:10:35 changed by JakeB

  • cc changed from tarmo@itech.ee, dkubb to tarmo@itech.ee, dkubb, JakeB.

(in reply to: ↑ 5 ) 05/03/07 21:23:34 changed by JakeB

Would the submitter consider converting their plugin into a patch and include test cases?

I've converted the plugin to a patch. In the process I uncovered some problems with the unit tests for ActiveRecord and submitted Ticket #8218. This patch doesn't test correctly unless the patch in Ticket #8218 is applied.

05/03/07 21:23:50 changed by JakeB

  • attachment ticket5369.patch added.

05/05/07 20:12:08 changed by JakeB

  • summary changed from validates_associated fails when associated id is incorrect to [PATCH] validates_associated fails when associated id is incorrect.

05/25/07 01:18:46 changed by lonestarsoftware

Ticket #8461 includes a documentation patch to help steer others away from this confusion.

08/21/07 02:02:10 changed by b.eggleston

I noticed using this patch with ActiveRecord 1.14.4 (Rails 1.1) there is a small problem. I had to change the line

class_to_check = reflection.options[:polymorphic] ? record.send(reflection.options[:foreign_type]).constantize : reflection.active_record

to read

class_to_check = reflection.options[:polymorphic] ? record.send(reflection.options[:foreign_type]).constantize : reflection.klass

otherwise it searches in the wrong table for the association.

08/21/07 02:02:59 changed by b.eggleston

  • cc changed from tarmo@itech.ee, dkubb, JakeB to tarmo@itech.ee, dkubb, JakeB, b.eggleston@gmail.com.

09/26/07 01:02:00 changed by stouset

+1.

This change needs to make it into core. Right now validates_associated is not only damn near useless, but doesn't even validate what the name implies it does.

(follow-up: ↓ 15 ) 09/26/07 01:02:27 changed by stouset

  • cc changed from tarmo@itech.ee, dkubb, JakeB, b.eggleston@gmail.com to tarmoitech.ee, dkubb, JakeB, b.eggleston@gmail.com, stephen@touset.org.
  • milestone set to 1.2.4.

(in reply to: ↑ 14 ) 12/03/07 01:01:03 changed by eggie5

Replying to stouset:

What's the status of this?

06/26/08 16:00:12 changed by mack

I'm also still curious about this. It looks to me like the behavior is still as described above - now in 2.1.

If this doesn't deserve being included in core - what is the reason? Are we simply, grossly misunderstanding the intent of validates_associated?