Make PredicateBuilder recognise association names #1736

Closed
ajrkerr opened this Issue Jun 16, 2011 · 11 comments

Comments

Projects
None yet
9 participants

ajrkerr commented Jun 16, 2011

With these models:

class Permission < ActiveRecord::Base
  belongs_to :group
end

class Group < ActiveRecord::Base
  has_many :permissions
end

Then trying to do this:

Permission.where( :group => Group.first )

Causes this:

: SELECT "permissions".* FROM "permissions"  WHERE "permissions"."group" = 1766
ActiveRecord::StatementInvalid: PGError: ERROR:  column permissions.group does not exist

This worked fine in Rails 3.0, and as far as I've been able to Google I haven't found any changes in behaviour noted elsewhere. Using Squeel if that makes a difference.

Member

jonleighton commented Jun 16, 2011

Please try it without any additional plugins and report back if you can reproduce the problem.

ajrkerr commented Jun 17, 2011

Yeah, issue is reproducible in a new Rails 3.1 app.

@ghost ghost assigned jonleighton Jun 17, 2011

Contributor

masterkain commented Jun 19, 2011

I can confirm this issue with Rails 3.1 (3.1-stable latest git) and MySQL:

ruby-1.9.2-p180 :001 > MediaObject.where(user: User.first)
User Load (0.5ms)  SELECT `users`.* FROM `users` LIMIT 1
MediaObject Load (0.5ms)  SELECT `media_objects`.* FROM `media_objects` WHERE `media_objects`.`hidden` = 0 AND `media_objects`.`user` = 1 ORDER BY media_objects.author ASC, media_objects.year ASC, media_objects.album ASC, media_objects.disc_number ASC, media_objects.position ASC
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'media_objects.user' in 'where clause': SELECT `media_objects`.* FROM `media_objects`  WHERE `media_objects`.`hidden` = 0 AND `media_objects`.`user` = 1 ORDER BY media_objects.author ASC, media_objects.year ASC, media_objects.album ASC, media_objects.disc_number ASC, media_objects.position ASC
Member

jonleighton commented Jun 23, 2011

Hello,

I am pretty sure this is not a regression. In 3.0 you can do Permission.where(:group_id => Group.first), but not Permission.where(:group => Group.first). See my test here: https://gist.github.com/6d4d9e8339058812dc75

Myself and @tenderlove both agree that the second format ought to work, so I'll leave this open and hopefully we can implement it for 3.2.

I think this feature is probably implemented in squeel or metawhere, which might have caused the confusion. Summoning @ernie in case he has thoughts :)

Cheers

Contributor

ernie commented Jun 23, 2011

Thanks, @jonleighton, and you're right.

MetaWhere did support ActiveRecord values. (see section on "Using ActiveRecord objects as condition values" here). I stripped the feature from Squeel, though.

When it came time to rewrite, I looked at the ugly hackery I pulled to make this happen, and decided in a fit of depression that it just wasn't worth it, for syntactic sugar. It did come in handy with polymorphic belongs_to, though.

If something similar goes into core in the future, I'll obviously follow suit with Squeel.

ajrkerr commented Jun 23, 2011

Thank you gentlemen for sorting this out and leaving it open for the future. My sincerest apologies for the confusion, I will be more thorough in my testing in the future before submitting an issue. I greatly appreciate the time and commitment you guys have for Rails development and the community =)

Contributor

plentz commented Oct 28, 2011

+1

DriesS commented Nov 16, 2011

+1

@rafaelfranca seems @tenderlove and @jonleighton have different opinions on this. This is a similar to issue to #5067. It appears #5067 (or this one) can be closed since they are duplicated. This one has more discussion though :D

Myself (@jonleighton) and @tenderlove both agree that the second format ought to work, so I'll leave this open and hopefully we can implement it for 3.2.

Owner

rafaelfranca commented Apr 29, 2012

@twinturbo thanks

Since this is more a feature request than a bug report, I'm closing in favor of continuing the discussion in the linked pull request.

In case the pull request does not get merged or goes forward, please feel free to start another discussion in the future using the core mailing list.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment