मैं अपने रिश्ते को पूरा करने की कोशिश कर रहा हूं लेकिन मुझे संघों का उपयोग करने में परेशानी हो रही है।रेल मॉडल has_many: संघों के माध्यम से
तो मेरे पास तीन मॉडल Workout
, Exercise
और WorkoutExercise
हैं। एक कसरत कई अभ्यास करना चाहिए था और एक व्यायाम अलग वर्कआउट इसलिए होना चाहिए मैं ने लिखा है:
class Workout < ActiveRecord::Base
has_many :workout_exercises
has_many :exercises, :through => :workout_exercises
end
class Exercise < ActiveRecord::Base
has_many :workout_exercises
has_many :workouts, :through => :workout_exercises
end
class WorkoutExercise < ActiveRecord::Base
belongs_to :exercise
belongs_to :workout
end
मैं कुछ परीक्षण चल रहा है, लेकिन परीक्षण एक बार गुजर नहीं कर रहे हैं मैं एक कसरत, व्यायाम बना सकते हैं और फिर उन्हें workout_exercise में शामिल होने के कक्षा। यह मुझे इस तरह की कसरत में अभ्यास का उपयोग करने देगा नहीं:
Workout.create
Exercise.create
WorkoutExercise.create(:workout => Workout.first, :exercise => Exercise.first)
work = Workout.first
work.exercises.count #This line causes the error: undefined method exercises
मेरे डेटाबेस तालिकाओं इस तरह दिखेगा:
class CreateWorkouts < ActiveRecord::Migration
def change
create_table :workouts do |t|
t.string :title
t.text :description
t.float :score
t.timestamps
end
end
end
class CreateExercises < ActiveRecord::Migration
def change
create_table :exercises do |t|
t.string :title
t.text :description
t.float :value
t.timestamps
end
end
end
class CreateWorkoutExercises < ActiveRecord::Migration
def change
create_table :workout_exercises do |t|
t.timestamps
end
end
end
जब मैं चलाने के इस परीक्षण में यह कहते हैं exercises
अनिर्धारित रहता है। क्या किसी के पास कोई विचार है?
क्या आपने अपनी माइग्रेशन चलाई है? कृपया हमें अपनी 3 टेबल दिखाएं। और मुझे लगता है कि आपको थोड़ी देर के लिए यान्हा के सुझाव को अनदेखा करना चाहिए। आपका कोड सही प्रतीत होता है इसलिए आपको इसे अभी बदलने की ज़रूरत नहीं है। आप कुछ और याद कर रहे हैं। – Ashitaka
@ आशिताका मैंने उपरोक्त सारणी को जोड़ा, क्या CreateWorkoutExercises तालिका के साथ कुछ करना होगा खाली है? यह मेरा पहला समय habtm का उपयोग कर कर रहा है। – trev9065
ठीक है, तो यह था। आप उन आईडी को याद कर रहे थे जिन्होंने दो तालिकाओं के बीच कनेक्शन स्थापित किया था। आप शायद अपने माइग्रेशन को फिर से बनाना चाहते हैं। मुझे लगता है कि 'रेक डीबी: रीसेट' नौकरी करेगा (हालांकि यह आपके सभी रिकॉर्ड हटा देगा)। – Ashitaka