This project is archived and is in readonly mode.
collection_singular_ids= method raises an exception when primary_key is string (Rails 3)
-
Robert Pankowecki
I will try :-)
-
Neeraj Singh
- State changed from new to open
This might not be a rails issue. Problem might line with Arel gem.
I setup a model called 'brake' which has brake_id, a string, as primary key.
I tested with arel and this is what I got:
brakes = Table(:brakes) brakes.where(brakes[:brake_id].eq('b1')) # the sql that was generated is SELECT "brakes"."brake_id", "brakes"."name", "brakes"."car_id" FROM "brakes" WHERE "brakes"."brake_id" = 0Looks like ARel does not like primary_key being string.
-
Robert Pankowecki
Sending patch and tests for this case.
-
Robert Pankowecki
Could not reproduce your statement about Arel
ruby-1.9.2-head > Role.primary_key => "name" ruby-1.9.2-head > role = Role.arel_table ruby-1.9.2-head > role.where(role[:name].eq('role')).to_sql => "SELECT \"roles\".\"name\", \"roles\".\"created_at\", \"roles\".\"updated_at\" FROM \"roles\" WHERE \"roles\".\"name\" = 'role'" -
Neeraj Singh
- Milestone set to 3.x
- Assigned user set to José Valim
I had wrong settings.
Your patch works.
+1
-
Robert Pankowecki
I just found that my patch brokes the method when primary_key is a number. I'm goinh to post soon a better version.
-
Robert Pankowecki
But I have one question before: How should the method behave when the given list of ids constains unexisting id? Raise an exception or just ignore it silently?
-
José Valim
Current behavior is to ignore silently, let's leave it that way. :)
-
Robert Pankowecki
I checked that current behavior is to raise an exception. It works like that for Rails 2.2.8 and Rails 3 so I will stay with current implementation which seems to more safe for me.
Rails 2.3.8 ActiveRecord::RecordNotFound: Couldn't find all Containments with IDs (1,3) (found 1 results, but was looking for 2) from /home/rupert/.rvm/gems/ruby-1.9.2-head@rails222/gems/activerecord-2.3.8/lib/active_record/base.rb:1643:in `find_some' from /home/rupert/.rvm/gems/ruby-1.9.2-head@rails222/gems/activerecord-2.3.8/lib/active_record/base.rb:1602:in `find_from_ids' from /home/rupert/.rvm/gems/ruby-1.9.2-head@rails222/gems/activerecord-2.3.8/lib/active_record/base.rb:619:in `find' from /home/rupert/.rvm/gems/ruby-1.9.2-head@rails222/gems/activerecord-2.3.8/lib/active_record/associations.rb:1337:in `block in collection_accessor_methods' Rails 3.0.0.beta4 ActiveRecord::RecordNotFound: Couldn't find all Containments with IDs (1, 3) (found 0 results, but was looking for 2) from /home/rupert/.rvm/gems/ruby-1.9.2-head/gems/activerecord-3.0.0.beta4/lib/active_record/relation/finder_methods.rb:316:in `find_some' from /home/rupert/.rvm/gems/ruby-1.9.2-head/gems/activerecord-3.0.0.beta4/lib/active_record/relation/finder_methods.rb:277:in `find_with_ids' from /home/rupert/.rvm/gems/ruby-1.9.2-head/gems/activerecord-3.0.0.beta4/lib/active_record/relation/finder_methods.rb:102:in `find' from /home/rupert/.rvm/gems/ruby-1.9.2-head/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:403:in `find' from /home/rupert/.rvm/gems/ruby-1.9.2-head/gems/activerecord-3.0.0.beta4/lib/active_record/associations.rb:1423:in `block in collection_accessor_methods' -
José Valim
Does this mean I can close this ticket?
-
Robert Pankowecki
No! I'm working on patch right at this moment to fix the behavior when the primary key is not integer. I will submit a patch in a seconds.
-
José Valim
Ok! :)
-
Robert Pankowecki
I'm sending a better patch with test for string and integer primary keys (should work fine with other types of primary keys as well).
-
Krzysztof Kuczek
Hi,
I've had similar problems, the patch seems to fix it. It just works. Thank you. -
Repository
- State changed from open to resolved
(from [f8b53f35b9cbf2a134a7d9184a044ce95764acfa]) test and fix collection_singular_ids= with string primary keys [#5125 collection_singular_ids= method raises an exception when primary_key is strin... state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/f8b53f35b9cbf2a134a7d9184a044c... -
Luis Correa d'Almeida
I've just checked out the 3.0.0.rc3 and the following does not work:
my_active_record_object.other_ids = ["uuid1", "uuid2"]It seems to still be casting those uuids to integers
ActiveRecord::RecordNotFound: Couldn't find Other with ID=0Can anyone else confirm this?
-
Luis Correa d'Almeida
And if you pass in a valid (existing) id, you get
ActiveRecord::AssociationTypeMismatch: Other(#28595860) expected, got NilClass(#645570) -
José Valim
Could you please be more clear? There is no Rails 3.0.0.rc3.
-
Luis Correa d'Almeida
Sorry, that was a typo - 3.0.0.rc
-
Luis Correa d'Almeida
I checked the tag and it seems that it was tagged before this commit. So it makes sense that it is still breaking in the 3.0.0_RC tag.
