2016-03-20 18 views
0
class Card < ApplicationRecord 
    has_one :card_rating 
    has_one :rating, through: :card_rating 
end 

class Rating < ApplicationRecord 
    has_many :card_ratings 
    has_many :cards, through: :card_ratings 
end 

class CardRating < ApplicationRecord 
    belongs_to :card 
    belongs_to :rating 
end 

ich etwas entlang der Linien der folgenden Aktionen ausführen möchten:Wie ein Objekt erstellen - hat man: durch

c = card.card_rating.new 
c << rating 

Aber es scheint nicht zu sein, jede Vereinigung geht überhaupt, da bereits bei der ersten Anweisung die folgende Fehlermeldung erhalte ich:

undefined method `new' for nil:NilClass 

Antwort

0

Sie brauchen nicht eine Tabelle zu viele Beziehung für eine beitreten:

class Card 
    belongs_to :rating 
end 

class Rating 
    has_many :cards 
end 

Eher würden Sie has_one :through verwenden, falls die Domäne diktiert, dass die Beziehung indirekt ist.

class Student 
    belongs_to :school 
    has_one :headmaster, through: :school 
end 

class School 
    has_many :students 
end 

class Headmaster 
    belongs_to :school 
    has_many :students, through: :school 
end 
Verwandte Themen