Forum: Ruby on Rails How would I find all "players" on an "active team"?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2c2f3ce7125411ff78a1d3e902aa2a4d?d=identicon&s=25 Bob Sanders (adistarmid)
on 2008-12-18 07:57
I'm trying to find all players on an active team, and this is just
stumping me.

I defined the "active team" by:

@active_team = Team.find(:all, :conditions => ["active=?", true])

Now, I'm trying to use in my loop:

@active_team.players

but that just gives me an "undefined method `players'". I know I'm
probably missing something really simple. Any idea?
16b26104470ae1b64958f3caa836c193?d=identicon&s=25 Danny Burkes (dburkes)
on 2008-12-18 08:22
> @active_team = Team.find(:all, :conditions => ["active=?", true])
>

The result of find(:all,...) is an array

> Now, I'm trying to use in my loop:
>
> @active_team.players
>

You want @active_team.each {|t| t.players}

or some such.
9770ee3431156709c728f6105dd98f0d?d=identicon&s=25 Bosko (Guest)
on 2008-12-18 08:24
(Received via mailing list)
Do you have:

has_many :players

in your Team model?

In order to access players in a team or team in Player object you must
define relations in the model. In the later case it should be:

belongs_to :team


On Dec 18, 7:57 am, Bob Sanders <rails-mailing-l...@andreas-s.net>
2c2f3ce7125411ff78a1d3e902aa2a4d?d=identicon&s=25 Bob Sanders (adistarmid)
on 2008-12-18 08:34
Danny Burkes wrote:
>> @active_team = Team.find(:all, :conditions => ["active=?", true])
>>
>
> The result of find(:all,...) is an array
>
>> Now, I'm trying to use in my loop:
>>
>> @active_team.players
>>
>
> You want @active_team.each {|t| t.players}
>
> or some such.

Hi Danny,

I tried the @active_team.each {|t| t.players}, but it's not working for
me. It's reading the contents of the Team model for some reason. Is
there an alternative way that might work?
This topic is locked and can not be replied to.