0

I made 'hospital_review' table and 'hospital_review_comments' table with 1:N relationship on ruby on rails. 'hospital_review' table's migration file is like this.

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

and 'hospital_review_comments's one is like this.

   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' table's model file is like this.

belongs_to :user
has_many :hospital_review_comments

'hospital_review_comments' table's one is like this.

belongs_to :user
belongs_to :hospital_review

I wanted to show each hospital review and the comments that are written on it, so I programmed the codes below in 'show.html.erb'.

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

and this is show action in controller file.

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

but the error occcured with message

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

I tried 'foregin_key' in hospital_review_comments table's model file, but it didn't work. I can't get the reason the error occurred. Plz help!

felipa
  • 1

2 Answers2

1

You are missing hospital_review_id in your hospital_review_comments table

pk-n
  • 568
  • 4
  • 11
0

I think you have wrongly added post_id in hospital_review_comments table, which should be hospital_review_id, that will to the job.

Else you can add the foreign_key option in the association as follows.

has_many :hospital_review_comments, foreign_key: 'post_id'

rakeshpatra
  • 683
  • 8
  • 24