2015-09-05 26 views
0

我有一個數據庫,其中許多'帖子'可以有很多'標籤'。我在帖子和標籤之間建立了一個名爲'post_tag'的聯結表 但post_tag中帖子和標籤的外鍵列名稱不同。將自定義外鍵分配給Rails Junction表

這是我在post_tag模式:

class PostTag < ActiveRecord::Base 
    self.table_name = "post_tag" 

    #defining columns in post_tag that are foreign keys for post and tag tables 
    belongs_to :post, :class_name => "Post", :foreign_key => "post_fk" 
    belongs_to :tag, :class_name => "Tag", :foreign_key => "tag_fk" 
end 

:foreign_key是我post_tag結外鍵列table.In post_tag這些外國關鍵領域的平等的主鍵在崗或名稱標籤表。 這裏的錯誤,我得到:

irb(main):001:0> p = post.find(1) 
irb(main):002:0> p.post_tags 
     PostTag Load (0.9ms) SELECT `post_tag`.* FROM `post_tag` WHERE `post_tag`.`post_id` = 1 
    Mysql2::Error: Unknown column 'post_tag.post_id' in 'where clause': SELECT `post_tag`.* FROM `post_tag` WHERE `post_tag`.`post_id` = 1 
    ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'post_tag.post_id' in 'where clause': SELECT `post_tag`.* FROM `post_tag` WHERE `post_tag`.`post_id` = 1 

我如何得到它停止尋找post_idpost_fk

感謝您的幫助!

回答

0

原來:foreign_key => "actual_foreign_key"已折舊。

應該用foreign_key: "actual_foreign_key"代替!

相關問題