I have the following associations in my Survey model:
has_many :survey_group_lists, -> { order 'sequence ASC, group_id ASC' }
has_many :groups, through: :survey_group_lists
I want to add where cluase to :groups association so it will return only active groups. I've tried something like this:
has_many :groups, -> { where(active: true) }, through: :survey_group_lists
but it returns me an error:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "slide_groups"
What I'm doing wrong?
Edit: I'm using Rails 5.
has_many :groups, -> {where('groups.active' => true)}, through: :survey_group_lists
This is correct.
Note that the lambda has to be the second argument , otherwise you will see syntax error, unexpected '\\n', expecting =>
.
尝试这个:
has_many :groups, through: :survey_group_lists, -> { where(groups: {active: true}) }
它应该是:
has_many :groups, through: :survey_group_lists, -> { where(groups: { active: true } ) }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.