ruby on rails - Empty? fails on ActiveRecord with a default_scope( :order) -


occasionally want check whether person model has organizations. straightforward enough; use @person.organizations.empty?. however, coupled default_scope (default_scope { order(:name) }), error:

activerecord::statementinvalid (pg::invalidcolumnreference: error: select distinct, order expressions must appear in select list line 1: ... "relationships"."person_id" = $1 order "organizat... ^

: select distinct 1 one "organizations" inner join "contracts" on "organizations"."id" = "contracts"."organization_id" inner join "relationships" on "contracts"."id" = "relationships"."contract_id" "relationships"."person_id" = $1 order "organizations"."name" asc limit 1):

i'm using postgres db , (abbreviated) model setup looks follows:

class organization < activerecord::base   has_many :contracts   has_many :people, -> { uniq }, :through => :contracts    default_scope { order(:name) } end  class person < activerecord::base   has_many :relationships, :inverse_of => :person   has_many :contracts, :through => :relationships   has_many :organizations, -> { uniq }, :through => :contracts end  class contract < activerecord::base   belongs_to :organization, touch: true    has_many :relationships, :inverse_of => :contract   has_many :people, :through => :relationships end 

things have tried far:

  has_many :organizations, -> { order(:name).uniq }, :through => :contracts 

which supposedly make activerecord see coming in advance (it didn't) and

  has_many :organizations, -> { includes(:name).uniq }, :through => :contracts 

which fixes problem when put in manually in console, not in app itself. how coerce activerecord formatting empty? query differently or dropping order whenever using empty??

edit: clear, aware using @person.organizations.count == 0 work, , there other one-off solutions. i'm looking general 1 don't have keep repeating myself.

@person.organizations.reorder('').empty?  

should work


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -