मैं रेल सीख रहा हूं और मैंने संबंधों के साथ एक समस्या को ठोकर दिया है। मेरे पास कई से अधिक एसोसिएशन उपयोगकर्ता हैं - टूर्नामेंट, और किसी कारण से मैं उपयोगकर्ता के उदाहरणों पर भाग लेने वाले_टोरमेंट्स तक नहीं पहुंच सकता, या टूर्नामेंट के उदाहरणों पर प्रतिभागियों तक नहीं पहुंच सकता।रेल, has_many: के माध्यम से, अपरिभाषित विधि `to_sym` शून्य के लिए: NilClass
2.0.0-p643 :001 > Tournament.new.participants
NoMethodError: undefined method `to_sym' for nil:NilClass
from /home/marcin/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.8/lib/active_record/reflection.rb:100:in `_reflect_on_association'
from /home/marcin/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.8/lib/active_record/reflection.rb:537:in `source_reflection'
उपयोगकर्ता मॉडल
class User < ActiveRecord::Base
has_many :participants_tournaments
has_many :participated_tournaments, :through => :participants_tournaments
end
टूर्नामेंट मॉडल
class Tournament < ActiveRecord::Base
has_many :participants_tournaments
has_many :participants, :through => :participants_tournaments
end
ParticipantsTournament मॉडल
class ParticipantsTournament < ActiveRecord::Base
belongs_to :tournament
belongs_to :user
end
ParticipantsTournament प्रवास
def change
create_table :participants_tournaments, :id => false do |t|
t.integer "tournament_id",
t.integer "user_id"
end
end
मैंने पढ़ लिया है: This topic on SO, also this topic और देखा/this railscast पढ़ा है, लेकिन मैं अंत में यह काम करना आरंभ नहीं कर पा रहे।
क्या आप 't = tournament.new' 't.participants' के बाद कोशिश कर सकते हैं? – sixty4bit
has_many के साथ: मैं बहुत निश्चित हूं कि जॉइन टेबल को प्राथमिक कुंजी के लिए आईडी कॉलम की आवश्यकता होती है, इसलिए जब आप उस तालिका को बनाते हैं, तो सुनिश्चित करें कि आप माइग्रेशन में 'id => false' को हटा दें। – maxhungry
@MaxHung आपको एक जॉइन टेबल में आईडी की आवश्यकता नहीं है। आप जिन चीज़ों में शामिल हो रहे हैं, उनके लिए आपको केवल आईडी की आवश्यकता है। – Austio