0
votes

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)

1
try event_spaces.virtual = true (plural)Alireza
why don't you do `Events.where(comapny_id: c.id).where("event_space.virtual = true")MZaragoza
@Alireza not working, the same errormaki
@MoisesZaragoza not working, we cannot access event_spaces in this way, we need to use joins in this case I thinkmaki
this should work: c.events.joins(:event_space).where(event_spaces: {virtual: true})Alireza

1 Answers

1
votes

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

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