2016-09-04 6 views
0

ruby​​ on railsで 'hospital_review'テーブルと 'hospital_review_comments'テーブルを1:Nの関係で作成しました。 'hospital_review'テーブルの移行ファイルは次のとおりです。ruby​​ on railsのhas_many-belongs_toテーブルが関連していないようです。

class CreateHospitalReviews < ActiveRecord::Migration 
def change 
    create_table :hospital_reviews do |t| 
     t.string :written_time 
     t.string :writter 
     t.integer :user_id 
     t.string :category 
     t.string :hospital_name 
     t.text :content 
     end 
    end 

'hospital_review_commentsのようなものです。

def change 
    create_table :hospital_review_comments do |t| 
     t.integer :user_id 
     t.integer :post_id 
     t.string :writter 
     t.string :written_time 
     t.text :content 

     t.timestamps null: false 
    end 
    end 

'hospital_review'テーブルのモデルファイルは次のとおりです。

belongs_to :user 
has_many :hospital_review_comments 

'hospital_review_comments'テーブルのようなものです。

belongs_to :user 
belongs_to :hospital_review 

私は、各病院の見直しとそれに書かれたコメントを表示したかったので、私は「show.html.erb」内の以下のコードをプログラムします。

<% @post.hospital_review_comments.each do |comment| %> 
<p><strong><%=comment.user.username%></strong> <%=comment.content%></p> 

これはコントローラファイルのshowアクションです。

def show 
@post = HospitalReview.find(params[:id]) 
@hospital_comment_writer = User.where(id: session[:user_id])[0] 
end 

が、エラーメッセージ

'SQLite3::SQLException: no such column:'. 

でocccured私はhospital_review_commentsテーブルのモデルファイルの 'foregin_key' を試してみましたが、それはうまくいきませんでした。エラーが発生した理由はわかりません。 Plzヘルプ!

答えて

1

あなたのhospital_review_commentsテーブル

+0

私はhospital_review_idにpost_idのを変更し、そして。今それは動作します!おかげで多くの – felipa

+0

あなたは私の答えを受け入れることができます。緑の目盛り。上下の投票セクションの下。 –

0

hospital_review_idを欠けている私はhospital_review_id、仕事への意志であるべき、あなたは間違ってhospital_review_commentsテーブルにpost_idを追加したと思います。

これ以外の場合は、foreign_keyオプションを次のように追加できます。

にhas_many:hospital_review_comments、FOREIGN_KEY: 'post_idの'

関連する問題