blee908
blee908

Reputation: 12555

Rails: Has Many... Through Syntax Issue With Where Scope

So I have two models, users and services. Users that check a service basically bookmarks it, so I want to access them via user.checks. Now there can be different relationships between a user and service, that's why I placed the where clause to make sure the relationship_type is check.

The Code

 has_many :checks, through: :user_services, source: :service, dependent: :destroy, -> { where relationship_type: 'check' }

The Error

/app/models/user.rb:21: syntax error, unexpected '\n', expecting =>

It's a syntax issue and I can't figure it out!

Upvotes: 0

Views: 50

Answers (1)

akhanubis
akhanubis

Reputation: 4232

When you pass a hash as parameter to a method, you can suppress the {} only when the hash is the last (or only) parameter passed. In this case, you pass a lamba after it, so you must explicitly write the hash as

has_many :checks, { through: :user_services, source: :service, dependent: :destroy }, -> { where relationship_type: 'check' }

Upvotes: 1

Related Questions