Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Habtm, :uniq option, ActiveRecord::QueryMethods #1424

Closed
TatianaTyu opened this Issue May 31, 2011 · 2 comments

Comments

Projects
None yet
3 participants

Hi guys,
Thank you for doing such a great job, I can't stop getting amazed by how talented and supportive each of you are.

Recently I came across an issue in Rails 3.0.7 which is maybe a bug, so I'm posting it here.

class Member < ActiveRecord::Base
  has_and_belongs_to_many :roles, :uniq => true
end

class Role < ActiveRecord::Base
  has_and_belongs_to_many :members, :uniq => true
end

If there is a duplicate in members_roles table, my_role.members won't return duplicates (as expected), but
my_role.members.where(...) will return duplicates despite :uniq => true setting.

Member

jonleighton commented May 31, 2011

Hello, thanks for the report. Please could you try the 3.1 RC and let me know if you still experience the bug?

Member

sikachu commented Jun 1, 2011

I'm sure that this behavior exists since Rails 3.0.1. In the normal case, it would be impossible for member_roles table to have the duplicate member_id and role_id. However, in the race condition it will.

By setting the :uniq option on has_and_belongs_to_many, it will not guarantee that you'll have non-duplicate roles/member in complex query. I would suggest you alter the member_roles table and make [member_id, role_id] as unique keys instead. That will surely prevent this type of duplicate in the future.

I'm closing this, unless you're going to provide a patch for it. Thanks again for reporting in.

@sikachu sikachu closed this Jun 1, 2011

@jake3030 jake3030 pushed a commit to jake3030/rails that referenced this issue Jun 28, 2011

@spohlenz @dhh spohlenz + dhh Allow helpers directory to be overridden via ActionController::Base.h…
…elpers_dir (Sam Pohlenz) [#1424 state:committed]

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
bdf995b

@jake3030 jake3030 pushed a commit to jake3030/rails that referenced this issue Jun 28, 2011

@spohlenz @dhh spohlenz + dhh Allow helpers directory to be overridden via ActionController::Base.h…
…elpers_dir (Sam Pohlenz) [#1424 state:committed]

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
5ea9f2c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment