{"action":"created","issue":{"url":"https://api.github.com/repos/thoughtbot/shoulda-matchers/issues/716","labels_url":"https://api.github.com/repos/thoughtbot/shoulda-matchers/issues/716/labels{/name}","comments_url":"https://api.github.com/repos/thoughtbot/shoulda-matchers/issues/716/comments","events_url":"https://api.github.com/repos/thoughtbot/shoulda-matchers/issues/716/events","html_url":"https://github.com/thoughtbot/shoulda-matchers/issues/716","id":75672310,"number":716,"title":"has_many test with a condition, where condition is a scope","user":{"login":"abitdodgy","id":545430,"avatar_url":"https://avatars.githubusercontent.com/u/545430?v=3","gravatar_id":"","url":"https://api.github.com/users/abitdodgy","html_url":"https://github.com/abitdodgy","followers_url":"https://api.github.com/users/abitdodgy/followers","following_url":"https://api.github.com/users/abitdodgy/following{/other_user}","gists_url":"https://api.github.com/users/abitdodgy/gists{/gist_id}","starred_url":"https://api.github.com/users/abitdodgy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/abitdodgy/subscriptions","organizations_url":"https://api.github.com/users/abitdodgy/orgs","repos_url":"https://api.github.com/users/abitdodgy/repos","events_url":"https://api.github.com/users/abitdodgy/events{/privacy}","received_events_url":"https://api.github.com/users/abitdodgy/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/thoughtbot/shoulda-matchers/labels/Feature%20Request","name":"Feature Request","color":"c7def8"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-05-12T17:13:35Z","updated_at":"2015-06-03T13:10:56Z","closed_at":null,"body":"Is there a way to test a has_many through relation with a condition, where the condition is a lambda pointing to a scope in another class?\r\n\r\nLet's assume we have a Membership model that joins a many to many relation between a User and a Workspace model. The membership has a role column.\r\n\r\n```\r\nclass Membership < ActiveRecord::Base\r\n  belongs_to :user\r\n  belongs_to :workspace\r\n\r\n  scope :admin, -> { where(role: :admin) }\r\n```\r\n\r\nWe want to get users with a 'admin' role:\r\n\r\n```ruby\r\nclass Workspace < ActiveRecord::Base\r\n  has_many :memberships\r\n  has_many :members, through: :memberships, source: :user\r\n  has_many :admins, -> { Membership.admin }, through: :memberships, source: :user\r\nend\r\n```\r\n\r\nIs there a way to test this? I know I can do it manually, but I'm curious to see if this is possible. I tried this:\r\n\r\n```ruby\r\nshould have_many(:admins).through(:memberships).conditions(memberships: { role: :admin })\r\n```\r\n\r\nWhich fails:\r\n\r\n```\r\n 1) Failure:\r\nWorkspaceTest#test_: Workspace should have many admins through memberships.  [/Users/Mohamad/.rvm/gems/ruby-2.2.0/gems/shoulda-context-1.2.1/lib/shoulda/context/context.rb:344]:\r\nExpected Workspace to have a has_many association called admins (admins should have the following conditions: {:memberships=>{:role=>:admin}})\r\n```"},"comment":{"url":"https://api.github.com/repos/thoughtbot/shoulda-matchers/issues/comments/108399249","html_url":"https://github.com/thoughtbot/shoulda-matchers/issues/716#issuecomment-108399249","issue_url":"https://api.github.com/repos/thoughtbot/shoulda-matchers/issues/716","id":108399249,"user":{"login":"abitdodgy","id":545430,"avatar_url":"https://avatars.githubusercontent.com/u/545430?v=3","gravatar_id":"","url":"https://api.github.com/users/abitdodgy","html_url":"https://github.com/abitdodgy","followers_url":"https://api.github.com/users/abitdodgy/followers","following_url":"https://api.github.com/users/abitdodgy/following{/other_user}","gists_url":"https://api.github.com/users/abitdodgy/gists{/gist_id}","starred_url":"https://api.github.com/users/abitdodgy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/abitdodgy/subscriptions","organizations_url":"https://api.github.com/users/abitdodgy/orgs","repos_url":"https://api.github.com/users/abitdodgy/repos","events_url":"https://api.github.com/users/abitdodgy/events{/privacy}","received_events_url":"https://api.github.com/users/abitdodgy/received_events","type":"User","site_admin":false},"created_at":"2015-06-03T13:10:56Z","updated_at":"2015-06-03T13:10:56Z","body":"@mcmire I'd like to close this issue. I think I made a mistake. The column I was using as a condition is an enum column. Using my example above, this works: (2 translates to :admin by Rails).\r\n\r\n```ruby\r\nshould have_many(:admins).through(:memberships).conditions(role: 2)\r\n```\r\n\r\nThe failing example had this:\r\n\r\n```ruby\r\nshould have_many(:admins).through(:memberships).conditions(memberships: { role: :admin })\r\n```\r\n\r\nSo this looks like a none issue. Thoughts, or can we close this?\r\n"}}