This project is archived and is in readonly mode.
uniqueness with accepts_nested_attributes_of
Reported by Ivan | November 5th, 2010 @ 08:36 PM
class Room < ActiveRecord::Base
has_many :objects, :dependent => :destroy, :inverse_of => :room
accepts_nested_attributes_for :objects, :allow_destroy => true
end
class Object < ActiveRecord::Base
belongs_to :room, :inverse_of => :objects
validates :forniture_id, :presence => true, :uniqueness => {:scope => :room_id}
validates :room, :presence => true
end
The "uniqueness" validation doesn't work. When the 'select' is issued, room_id is still NULL, it was not yet set by Rails.
Comments and changes to this ticket
-
Neeraj Singh November 6th, 2010 @ 06:59 AM
- Importance changed from to Low
Can you elaborate what you mean by when the 'select' is issued?
Can you show me the full case using console?
Thanks
-
Ivan November 8th, 2010 @ 11:38 AM
Sorry, I'd like to say the select 'generated' by Rails to verify if the key record already exists.
In the example, 'room_id' would be 'nil', because it wasn't set by Rails yet.
If you look in the log you can observe that SQL generated is:SELECT
objects
.id
FROMobjects
WHERE (objects
.forniture_id
= 10) AND (objects
.room_id
IS NULL) LIMIT 1So, even if you have 2 'Object' with the same 'forniture_id' being inserted into the same Room, the result would be as it has none.
Thanks
-
Neeraj Singh November 10th, 2010 @ 09:12 PM
- State changed from new to duplicate
There is already a ticket for this one.
https://rails.lighthouseapp.com/projects/8994/tickets/2646-validati...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>