2012-03-10 13 views
7

मैं अपने रिश्ते को पूरा करने की कोशिश कर रहा हूं लेकिन मुझे संघों का उपयोग करने में परेशानी हो रही है।रेल मॉडल 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 अनिर्धारित रहता है। क्या किसी के पास कोई विचार है?

+0

क्या आपने अपनी माइग्रेशन चलाई है? कृपया हमें अपनी 3 टेबल दिखाएं। और मुझे लगता है कि आपको थोड़ी देर के लिए यान्हा के सुझाव को अनदेखा करना चाहिए। आपका कोड सही प्रतीत होता है इसलिए आपको इसे अभी बदलने की ज़रूरत नहीं है। आप कुछ और याद कर रहे हैं। – Ashitaka

+0

@ आशिताका मैंने उपरोक्त सारणी को जोड़ा, क्या CreateWorkoutExercises तालिका के साथ कुछ करना होगा खाली है? यह मेरा पहला समय habtm का उपयोग कर कर रहा है। – trev9065

+0

ठीक है, तो यह था। आप उन आईडी को याद कर रहे थे जिन्होंने दो तालिकाओं के बीच कनेक्शन स्थापित किया था। आप शायद अपने माइग्रेशन को फिर से बनाना चाहते हैं। मुझे लगता है कि 'रेक डीबी: रीसेट' नौकरी करेगा (हालांकि यह आपके सभी रिकॉर्ड हटा देगा)। – Ashitaka

उत्तर

10

ठीक है, इसलिए आपकी वर्कआउट एक्साइसेस तालिका खाली नहीं हो सकती है।

class CreateWorkoutExercises < ActiveRecord::Migration 
    def change 
    create_table :WorkoutExercises do |t| 
     t.integer :exercise_id, :null => false 
     t.integer :workout_id, :null => false 

     t.timestamps 
    end 

    # I only added theses indexes so theoretically your database queries are faster. 
    # If you don't plan on having many records, you can leave these 2 lines out. 
    add_index :WorkoutExercises, :exercise_id 
    add_index :WorkoutExercises, :workout_id 
    end 
end 

इसके अलावा, आप इस तालिका जो भी आप करना चाहते हैं नाम कर सकते हैं, यह WorkoutExercises होने की जरूरत नहीं है: यह है कि यह कैसे दिखना चाहिए है। हालांकि, यदि आप has_and_belongs_to_many रिश्ते का उपयोग कर रहे थे, तो आपकी तालिका को अनिवार्य रूप से व्यायाम व्यायाम किया जाना होगा। ध्यान दें कि व्यायाम कसरत से पहले कैसे आता है। नामों को वर्णानुक्रम से आदेश दिया जाना चाहिए। मुझसे मत पूछो क्यों, यह सिर्फ एक रेल सम्मेलन है।

तो, इस मामले में, आप अपनी तालिका के साथ वर्कआउटएक्स व्यायाम का नाम ठीक करेंगे। लेकिन अगर मैं तुम थे, तो मैं इसे व्यायाम के लिए बदल दूंगा, बस मामले में, ताकि आप इसे कभी गलत न करें।

+1

धन्यवाद @ अष्टिका। रेल 4 में, कॉलम और इंडेक्स माइग्रेशन को एक पंक्ति में लिखा जा सकता है। Trereferences: व्यायाम, अनुक्रमणिका: true' – scarver2

1

आपका कोड ठीक दिखता है। बग शायद has_and_belongs_to_many एक बेहतर विकल्प है। Choosing Between has_many :through and has_and_belongs_to_many

+0

मेरा मानना ​​है कि has_and_belongs_to_many संगठनों को बहिष्कृत किया जा रहा है। http://stackoverflow.com/questions/7850111/rails-3-1-has-and-belongs-to-many-deprecated – Ben

संबंधित मुद्दे