T1djani
T1djani

Reputation: 27

invalid reference to FROM-clause entry for table

i have this error when i want to search with params and order too in list :

ERROR: invalid reference to FROM-clause entry for table "customers"

HINT: Perhaps you meant to reference the table alias "customers_contract_periods".

Here's my query in rails 4.2.1 : Before i do this :

order = "CONCAT(customers.company_name, customers.last_name)"

and :

contract_periods = ContractPeriod.current
                                 .joins( :contract, :customer )
                                 .merge( Contract.search params[ :search ] )
                                 .select( 'contract_periods.*' )
                                 .where( filter_params )
                                 .reorder( order, :start_on )

and :

contract_periods = contract_periods.merge ContractPeriod.by_state params[ :by_state ] if params[ :by_state ]

contract_periods = contract_periods.paginate( page: params[ :page ],
                                          per_page: params[ :per_page ])


contracts = Contract.includes( :contract_type, :customer )
                    .where( id: contract_periods.map(&:contract_id) )

contract_types = contracts.map( &:contract_type ).uniq
customers = contracts.map( &:customer )

Model associations :

  class Customer < ActiveRecord::Base

  ## Associations
  belongs_to  :billing_address,   class_name: 'Address'
  has_many    :contacts, as: :contactable

  has_many :contracts

class Contract < ActiveRecord::Base

belongs_to :contract_type
belongs_to :customer
has_many   :contract_periods

class ContractPeriod < ActiveRecord::Base

## Associations
belongs_to :contract
has_one    :customer,   through: :contract

Upvotes: 2

Views: 2933

Answers (1)

Triveni Badgujar
Triveni Badgujar

Reputation: 941

Whenever you refer any associated table in query you have to mention it as .references(:contracts) at the end of code.

Upvotes: 1

Related Questions