How can I create a scope in a model that returns only those objects with an associated rank greater than the current_user's rank?

China☆狼群 提交于 2019-12-08 08:38:47
dj.

And the answer is quite simple, after much experimenting:

scope :readable, lambda { |current_person| joins(:role_readable)
      .where(["roles.rank >= ?", current_person.roles.first.rank]) }

Although the join refers to the association name :role_readable, the where must use the table name — ie. roles.rank not role_readable.rank.

A trap for new players? Apparently.

Kudos to ctcherry and his answer here which gave the game away.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!