Bir Ekip modelim ve bir Fikstür modelim var. Fikstür modelinin bir takımı ve bir ev takımı var. Örneği this answer'da takip ettim ve birçok şeyin çalışmasını sağladım.Raylar has_many özel ActiveRecord Association
class Fixture < ActiveRecord::Base
belongs_to :home, class_name: 'Team'
belongs_to :away, class_name: 'Team'
end
class Team < ActiveRecord::Base
has_many :home_games, :class_name => 'Fixture', :foreign_key => 'home_id'
has_many :away_games, :class_name => 'Fixture', :foreign_key => 'away_id'
end
Şu anda ekipler demirbaşlar her bir listesini almak için team.fixtures @ çağırmak isterler @ team.home_games bana ev demirbaşlar verir ve team.away_games @ bana uzağa verir. has_many :games
'u has_many :home_games
'a benzer şekilde nasıl yazabilirim? Bunu yapmanın en iyi yolu bu mu? Takım modelinde
:
Team.first.games
#=> [<Fixture id: ... >, <Fixture id: ... >, ... ]
:
def games
Fixture.where("home_id = ? OR away_id = ?", self.id, self.id)
end
düzenli yöntemle gibi kullanın
iyi görünüyor, teşekkürler! –