Kev Jackson
7/20/2006 1:19:00 AM
>
> I've added some accessors because I don't know if they are needed
> by ActiveRecord, but nothing changes.
>
> class Player < ActiveRecord::Base
>
> attr_reader :team_id, :name, :age, :goal, :defence, :stamina, :goalkee
> per,
> :playmaking, :pass, :shot, :speed, :head, :creativity, :
> free_kicks,
> :captain, :mood, :aggresiveness
>
> attr_writer :team_id, :name, :age, :goal, :defence, :stamina, :goalkee
> per,
> :playmaking, :pass, :shot, :speed, :head, :creativity, :
> free_kicks,
> :captain, :mood, :aggresiveness
> belongs_to :team
>
> def initialize
> # De momento ponemos cualquier cosa, un numero aleatorio como
> cadena
> @name = (rand * 1000000).truncate.to_s
> # Edad entre 20 y 34 años
> @age = rand(34) + 1 + 20
> end
>
> # Crea un jugador con los valores por defecto
> def Player.create_player(team_id)
> player = Player.new
> player.team_id = team_id
> player.save
> end
>
> # Relaciona el jugador con un equipo
> def relate_to_team(team_id)
> @team_id = team_id
> end
> end
>
just try
class Player < ActiveRecord::Base
belongs_to :team
end
1 - you don't need to specify properties, ActiveRecord gets them from
the table definition, so no attr_reader etc
2 - you don't need to add a method to save the player, as it inherits
all the methods from ActiveRecord
If you can save a standard ActiveRecord-based object, then you can
change it and add instance/class methods later
--
"Man is truly free only among equally free men" - Michael Bakunin