2013-06-04 44 views
0

在我的數據庫中,我試圖獲得網點和文章之間的一對多關係。
我得到當使用這種關係以下錯誤:Rails Unidentified Method問題:undefined方法`outlet_id'

undefined method `outlet_id' for #<Article:0x007fc353887e58> 

下面是型號:

class Article < ActiveRecord::Base 
     belongs_to :analyst 
     belongs_to :outlet 
     has_and_belongs_to_many :loe 
     attr_accessible :article_body, :author, :distribution, :loe, :most_important, :pubdate, :publication, :state, :submitted, :summary, :title, :url, :analyst_id, :loe_ids, :outlet_id 
    end 

    class Outlet < ActiveRecord::Base 
     has_many :articles, foreign_key: :title 
     attr_accessible :distribution, :name, :state, :article_ids 
    end 

以下是架構:

create_table "articles_loes", :id => false, :force => true do |t| 
    t.integer "article_id" 
    t.integer "loe_id" 
    end 

    create_table "loes", :force => true do |t| 
    t.string "name" 
    t.string "customer" 
    t.integer "article_id" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    end 

    add_index "loes", ["article_id"], :name => "index_loes_on_article_id" 

    create_table "outlets", :force => true do |t| 
    t.string "name" 
    t.integer "articles_id" 
    t.integer "distribution" 
    t.string "state" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    end 

    add_index "outlets", ["articles_id"], :name => "index_outlets_on_articles_id" 

這裏是請撥:outlet

<div class="span4"> 
    <%= f.association :loe %> 
    <%= f.association :outlet %> 
    </div> 

如果有人有任何想法,我會很感激他們。我想我可能需要一篇關於Outlets的文章索引?如果情況確實如此,我並不確定如何實施。提前致謝。

+0

我想你已經錯誤地將「articles_id」添加到網點表中。您需要改爲在articles表中使用outlet_id。 –

回答

2

現在,您的Outlet模型無法與其擁有的articles關聯。一旦你說belongs_to,你需要有一個outlet_idcolumn。因此,您需要將outlet_id(整數)列添加到您的Article模型中,並使用它們所屬出口的ID填充它。如果在這種情況下某篇文章可能屬於多個網點,則需要通過聯合表創建一個many-to-many關係。

相關問題