I've got a organization model and an organization_profile model. The organization_profile model has an approved column. I would like to be able to find all approved users by calling something like: Organization.approved. I found that the best way to handle this is probably through scope. However I can't seem to get it to work. This is what i am trying
scope :approved, -> {joins(:organization_profile).where('organization_profile.approved = ?', true) }
But then Organization.approved gives me all kinds of errors:
Organization Load (8.0ms) SELECT "organizations".* FROM "organizations" INNER JOIN "organization_profiles" ON "organization_profiles"."user_id" = "organizations"."id" WHERE (organization_profile.approved = 't')
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "organization_profile"
LINE 1: ...profiles"."user_id" = "organizations"."id" WHERE (organizati...
^
: SELECT "organizations".* FROM "organizations" INNER JOIN "organization_profiles" ON "organization_profiles"."user_id" = "organizations"."id" WHERE (organization_profile.approved = 't')
Can anyone tell me the correct code?
Your query is using organization_profile
(singular) but your table name is organization_profiles
(plural).
A slightly better way to do this (which also avoids using strings), is to turn the where clause into an Arel predicate (might not be the right word):
scope :approved, -> { joins(:organization_profile).where(OrganizationProfile.arel_table['approved'].eq(true)) }
条件是SQL代码,该代码应正确反映始终为复数形式的表名:
where('organization_profiles.approved = ?', 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.