Rails PG::UndefinedTable: ERROR: missing FROM-clause entry for table

前端 未结 1 801
执念已碎
执念已碎 2020-12-29 04:05

I have models

class Offer < ActiveRecord::Base
  belongs_to :agency
end

class Agency < ActiveRecord::Base
  has_many :offers
end

Whe

相关标签:
1条回答
  • 2020-12-29 04:44

    The only thing you've added is a where-clause. Seems like it's problematic:

    where(agency: {state: 'active'})
    

    ...and produces a wrong condition because hash key in this case is expected to mean a table name. You should probably have this:

    where(agencies: {state: 'active'})
    

    Update

    Seeing this attract quite a lot of attention, I think I should also suggest a different way of doing the same, slightly more composable:

    merge( Agency.where(state: 'active') )
    

    How is this one better? It makes no assumptions about the table name of the model (not that it matters most of the time) and allows you to use scopes:

    # Inside Agency
    scope :active, -> { where(state: 'active') }
    
    # Somewhere else
    merge(Agency.active)
    
    0 讨论(0)
提交回复
热议问题