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

blee908

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!

akhanubis

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' }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails 4 has many through and order scope

From Dev

Rails 4 - has_many through fetching issue

From Dev

rails scope through has_many association

From Dev

Rails 4 has_many through many

From Dev

Has many through relationship in rails

From Dev

Pundit Policy Scope for Has Many Through Relationship

From Dev

Weird issue has_many through association in updated Rails

From Dev

Rails Active Record, get related record from has_many :through relationship with where clause on through record

From Dev

'where' condition in has_many_through in kohana

From Dev

Where with a has_many through

From Dev

nested queries for has_many :through in rails

From Dev

Rails Enum issue on Has_many, through...method cannot be called from my controller

From Dev

Rails 4 has_many through with where clause deletes multiple associations

From Dev

rails scope for follower_id ( has_many through source )

From Dev

Rails has_many :through with the where clause

From Dev

rails scope through has_many association

From Dev

Rails has many through not working

From Dev

has_many and has_many through with a where clause

From Dev

Rails Active Record, get related record from has_many :through relationship with where clause on through record

From Dev

Rails has many through relationship

From Dev

Active admin has many through relation issue

From Dev

Where with a has_many through

From Dev

Issue with join table and has_many through association in Rails 4

From Dev

Rails has_many through association with inheritance issue

From Dev

rails scope for follower_id ( has_many through source )

From Dev

Additive scope conditions for has_many :through

From Dev

Rails Model Associations with has_many, through, class_name and where clause

From Dev

Rails has_many through association issue

From Dev

rails has_many :through or has_many_belongs_to

Related Related

  1. 1

    Rails 4 has many through and order scope

  2. 2

    Rails 4 - has_many through fetching issue

  3. 3

    rails scope through has_many association

  4. 4

    Rails 4 has_many through many

  5. 5

    Has many through relationship in rails

  6. 6

    Pundit Policy Scope for Has Many Through Relationship

  7. 7

    Weird issue has_many through association in updated Rails

  8. 8

    Rails Active Record, get related record from has_many :through relationship with where clause on through record

  9. 9

    'where' condition in has_many_through in kohana

  10. 10

    Where with a has_many through

  11. 11

    nested queries for has_many :through in rails

  12. 12

    Rails Enum issue on Has_many, through...method cannot be called from my controller

  13. 13

    Rails 4 has_many through with where clause deletes multiple associations

  14. 14

    rails scope for follower_id ( has_many through source )

  15. 15

    Rails has_many :through with the where clause

  16. 16

    rails scope through has_many association

  17. 17

    Rails has many through not working

  18. 18

    has_many and has_many through with a where clause

  19. 19

    Rails Active Record, get related record from has_many :through relationship with where clause on through record

  20. 20

    Rails has many through relationship

  21. 21

    Active admin has many through relation issue

  22. 22

    Where with a has_many through

  23. 23

    Issue with join table and has_many through association in Rails 4

  24. 24

    Rails has_many through association with inheritance issue

  25. 25

    rails scope for follower_id ( has_many through source )

  26. 26

    Additive scope conditions for has_many :through

  27. 27

    Rails Model Associations with has_many, through, class_name and where clause

  28. 28

    Rails has_many through association issue

  29. 29

    rails has_many :through or has_many_belongs_to

HotTag

Archive