Miguel Morujão
Miguel Morujão

Reputation: 1161

Rails Join Query Error

i'm struggling to make a query that shows the friends of user that have activities in a city. The variable @friends has the friends of the user, and i want to make a join with activities and and places because i want to filter the results by city. A account(user) has many activities and also has many places though activities. I'm having the following error:

SQLite3::SQLException: no such column: accounts.city:

   def show
        @friends = Account.find(current_account.id).active_friends
        @nearbyFriends = @friends.joins(:activities,:places).where(:city => @place.city).group(:account_id)
      end

Upvotes: 0

Views: 28

Answers (1)

blelump
blelump

Reputation: 3243

Try:

def show
  # ...
  @nearbyFriends = @friends.joins(:activities,:places).where(:places => { :city => @place.city }).group(:account_id)
end

When using joins, any field in where clause must contain table name it comes from. The same applies to group (if friends table doesn't contain account_id).

Upvotes: 1

Related Questions