Skip to content Skip to sidebar Skip to footer

Rails Pg::undefinedtable: Error: Missing From-clause Entry For Table

I have models class Offer < ActiveRecord::Base belongs_to :agency end class Agency < ActiveRecord::Base has_many :offers end When I do such request - everything is OK @

Solution 1:

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)

Post a Comment for "Rails Pg::undefinedtable: Error: Missing From-clause Entry For Table"