Select from 3 tables! has_many throug

class Place < ActiveRecord::Base
attr_accessible :address, :city, :name, :description
has_many :meetings
end

Class Meeting < ActiveRecord::Base
attr_accessible :start_at, :place_id, :title, :end_at

belongs_to :place
has_many :participations
has_many :players, :through => :participations

end

class Participation < ActiveRecord::Base
attr_accessible :meeting_id, :player_id
end

class Player < ActiveRecord::Base
attr_accessible :id, :city, :first_name, :gear, :last_name, :email,
:password, :password_confirmation

has_many :participations
has_many :meetings, :through => :participations
end

How to select each meeting in the specified city together with its
address and amount of players?

Something like SELECT places.id AS place_id, places.name,
places.address, meetings.title, meetings.start_at, meetings.end_at,
meetings.id AS meeting_id from places, meetings where meetings.place_id
= places.id AND places.city = ‘City’;

but this query lacks amount of players.

On 9 October 2012 17:29, Eugeni K. [email protected] wrote:

has_many :players, :through => :participations

end

class Participation < ActiveRecord::Base
attr_accessible :meeting_id, :player_id

You need belongs_to metting and player here.

how to select all meetings in the specified city together with their
addresses and players?

By a city do you mean a Place or a string in player.city?

Colin

Colin L. wrote in post #1079115:

On 9 October 2012 17:29, Eugeni K. [email protected] wrote:

has_many :players, :through => :participations

end

class Participation < ActiveRecord::Base
attr_accessible :meeting_id, :player_id

You need belongs_to metting and player here.
Sure thing, sorry cleaned it up by mistake.

how to select all meetings in the specified city together with their
addresses and players?

By a city do you mean a Place or a string in player.city?

Colin

I mean a Place, as a Meeting occurs in some Place, so it will be all
Meetings in all Places filtered out by the city.
Also I discover that it is possible to use counter_cache to have a
number of Players, who joined the Meeting in the player_cache column, is
that right?

On 9 October 2012 18:53, Eugeni K. [email protected] wrote:

Sure thing, sorry cleaned it up by mistake.

how to select all meetings in the specified city together with their
addresses and players?

By a city do you mean a Place or a string in player.city?

Colin

I mean a Place, as a Meeting occurs in some Place, so it will be all
Meetings in all Places filtered out by the city.

You are using the word city again, so once again it is not clear what
you mean.

Back to the original question, if you have a place, @place for
example, then its meetings are @place.meetings and for each meeting
the players are meeting.players. You have also asked for the address
of the meeting, but you know that already as it is in @place.

Also I discover that it is possible to use counter_cache to have a
number of Players, who joined the Meeting in the player_cache column, is
that right?

I have not used counter_cache but i believe it can be used for such
things.

Colin