2009-06-03 9 views
37

मुझे has_many through काम करने के लिए एसोसिएशन प्राप्त करने में समस्याएं हैं।रेल: हैसनी थ्रूएऑसिएशन नॉटफाउंड एरर

मैं इस अपवाद बार आ रही है:

Article.find(1).warehouses.build 
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :entries in model Article 

ये शामिल मॉडल हैं:

class Article < ActiveRecord::Base 
    has_many :warehouses, :through => :entries 
end 

class Warehouse < ActiveRecord::Base 
    has_many :articles, :through => :entries 
end 

class Entry < ActiveRecord::Base 
    belongs_to :article 
    belongs_to :warehouse 
end 

और ये मेरे स्कीमा है:

create_table "articles", :force => true do |t| 
    t.string "article_nr" 
    t.string "name" 
    t.integer "amount" 
    t.string "warehouse_nr" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "unit" 
end 

create_table "entries", :force => true do |t| 
    t.integer "warehouse_id" 
    t.integer "article_id" 
    t.integer "amount" 
end 

create_table "warehouses", :force => true do |t| 
    t.string "warehouse_nr" 
    t.string "name" 
    t.integer "utilization" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
end 

उत्तर

108

आप

जोड़ने की जरूरत
has_many :entries 

आपके प्रत्येक मॉडल के लिए, चूंकि विकल्प के माध्यम से केवल एक दूसरा एसोसिएशन निर्दिष्ट करता है जिसे इसे दूसरी तरफ खोजने के लिए उपयोग करना चाहिए।

+2

आप तालिका तालिका संबंध में ** t.integer "warehouse_id" ** के बजाय ** t.references: गोदाम,: null => false ** का उपयोग कर सकते हैं ... आजकल रेल-लिश। – carlosayam

+1

इस परिवर्तन के साथ मुझे 'नाम त्रुटि: अनियमित निरंतर अनुच्छेद :: प्रविष्टि' मिलती है? – Meekohi

1

@Meekohi इसका मतलब है कि आपके पास कोई प्रविष्टि मॉडल नहीं है। मुझे अभी त्रुटि संदेश मिला है, इसलिए इसे इंगित करना चाहता था (कम प्रतिष्ठा के कारण इसे टिप्पणी के रूप में पोस्ट नहीं कर सकता)।

class Entry < ActiveRecord::Base 
    belongs_to :article 
    belongs_to :warehouse 
end 

, बस चलाने

rails g model Entry 
0

आप प्रत्येक मॉडल के लिए

has_many :entries 

जोड़ने के लिए और has_many ऊपर की आवश्यकता होगी,: के माध्यम से, इस तरह:

class Article < ActiveRecord::Base 
    has_many :entries 
    has_many :warehouses, :through => :entries 
end 

class Warehouse < ActiveRecord::Base 
    has_many :entries 
    has_many :articles, :through => :entries 
end 

अधिक विस्तृत दृश्य और con को संभालने के तरीके पर ट्यूटोरियल ट्रॉलर्स https://kolosek.com/rails-join-table/

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