Forum: Ruby on Rails Select from 3 tables! has_many throug

Posted by Eugeni Kurtov (lessless)
on 2012-10-09 18:29
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.
Posted by Colin Law (Guest)
on 2012-10-09 18:45
(Received via mailing list)
On 9 October 2012 17:29, Eugeni Kurtov <lists@ruby-forum.com> 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
Posted by Eugeni Kurtov (lessless)
on 2012-10-09 19:53
Colin Law wrote in post #1079115:
> On 9 October 2012 17:29, Eugeni Kurtov <lists@ruby-forum.com> 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?
Posted by Colin Law (Guest)
on 2012-10-09 21:48
(Received via mailing list)
On 9 October 2012 18:53, Eugeni Kurtov <lists@ruby-forum.com> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.