我想从类'attributes
创建一个列表我是ruby的新手 - 我有一个名为fixture的activerecord类,以及“Home Team”,“Draw”,“Away team”的数组,其中主队和客队都是Fixture表中的字段< / p>
我已经在Fixture类中提出了以下代码 - 如何访问类的值?
self.fix_list = [home_team.title, "Draw", away_team.title]
答案 0 :(得分:0)
我认为“Fixture”对于模型而言是一个非常糟糕的名称。反正:
class Fixture < ActiveRecord::Base
belongs_to :home_team, :class_name => "Team", :foreign_key => "home_team_id"
belongs_to :away_team, :class_name => "Team", :foreign_key => "away_team_id"
named_scope :with_team, lambda { |team_id| { :conditions => ['(home_team_id = ?
or away_team_id = ?)', team_id, team_id]} }
def fix_list
[home_team.title, "Draw", away_team.title]
end
end
class Team < ActiveRecord:Base
def fixtures
Fixture.with_team(id)
end
end
当然,在迁移“灯具”表时,您需要创建名为home_team_id
和away_team_id
的列。
编辑:我只是注意到has_many
中我最初的Team
关联没用。那named_scope
未经测试,我现在要睡觉了!