user573335
user573335

Reputation: 392

active record 3 select with nested tables

i've got these classes:

class Game < ActiveRecord::Base
    has_many :offers
    #table has an integer-column 'season'
end

class Broker < ActiveRecord::Base
    has_many :offers
end

class Offer < ActiveRecord::Base
    belongs_to :game
    belongs_to :broker
end

and i want to select all offers from one broker where the season of the game is 2009, for example. i tried

Broker.first.offers.joins(:game).where(:game => {:season => 2009}).each do |o|
    puts o.inspect
end

but that gives me

`rescue in log': PGError: ERROR: missing FROM-clause entry for table "game" (ActiveRecord::StatementInvalid) LINE 1: ...games" ON "games"."id" = "offers"."game_id" WHERE "game"."se... : SELECT "offers".* FROM "offers" INNER JOIN "games" ON "games"."id" = "offers"."game_id" WHERE "game"."season" = 2009 AND ("offers".broker_id = 1)

how should i do such selects, or where can i find more information on this?

Upvotes: 1

Views: 1931

Answers (1)

Teoulas
Teoulas

Reputation: 2963

Change where(:game => {:season => 2009}) to where(:games => {:season => 2009})

Your table is named "games" (in plural) and the hash key in the where condition should be the table name, not the association name.

Upvotes: 13

Related Questions