maki
maki

Reputation: 535

rails select from association using join

I have three models: company, event, event_space

company has many events event belongs to event space

now I want to get all events from a company where the event_space has virtual attribute set to true

c = Comapny.first
c.events.joins(:event_space).where("event_space.virtual = true")

I'm doing something wrong because I have

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: event_space.virtual: SELECT "events".* FROM "events" INNER JOIN "event_spaces" ON "event_spaces"."id" = "events"."event_space_id" WHERE "events"."company_id" = 2 AND (event_space.virtual = true)

Upvotes: 0

Views: 465

Answers (1)

Alireza
Alireza

Reputation: 2691

You can modify your where clause as follows to get it right:

c.events.joins(:event_space).where(event_spaces: {virtual: true})

Upvotes: 1

Related Questions