Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

on a has_many :through association, replacing conditions with lambda

I have this association:

has_many :foo_participators, through: :foos, source: :user, conditions: "foos.state = 'completed'"

Rails tells me:

DEPRECATION WARNING: The following options in your Bar.has_many :foo_participators declaration are deprecated: :conditions. Please use a scope block instead. For example, the following:

has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment'

should be rewritten as the following:

has_many :spam_comments, -> { where spam: true }, class_name: 'Comment'

Is this possible with my association?

like image 513
John Bachir Avatar asked Nov 27 '13 06:11

John Bachir


1 Answers

Figured this out right after I asked the question. For some reason I hadn't tried putting the lambda first -- doing so works perfectly.

has_many :foo_participators, ->{where "foos.state = 'completed'"}, through: :foos, source: :user
like image 138
John Bachir Avatar answered Sep 29 '22 03:09

John Bachir