user1107922
user1107922

Reputation: 610

Rails: Unknown attribute during model create

I have an error message - "unknown attribute :news_id" but I can't understand where the problem is. I have news controller and I want to create comments for every news. I hope that someone could help me. Thanks in advance.

schema.rb

create_table "comments", :primary_key => "ID", :force => true do |t| 
  t.integer "Author_ID" 
  t.integer "News_ID", :null => false 
  t.string "Content", :limit => 500, :null => false 
  t.datetime "Date", :null => false 
end

Comment model:

belongs_to :news

News model:

has_many :comments

Upvotes: 8

Views: 26524

Answers (3)

Promise Preston
Promise Preston

Reputation: 28800

I faced this same issue when working on a Ruby on Rails application with a PostgreSQL database in production.

Here's how I solved it:

The issue was that I added some new columns to the table(s) in my development environment using new migration files that I generated, but when I made a push to the production environment, I didn't create those new columns via migration too.

All I had to do was to simply run a database migration in the production environment to create those new columns using the migration files that were generated in the development environment.

rails db:migrate

That's all.

I hope this helps

Upvotes: 1

maximus ツ
maximus ツ

Reputation: 8065

This is because you have not added :news_id to your Comment's model.

Write the migration to add news_id to Comment and your problem will be solved.

Upvotes: 8

Rahul garg
Rahul garg

Reputation: 9362

You can print out the params by puts params in starting of your create action to check the actual attributes it is sending.

or you can check out the routes you are having for comment create action to get the parameter..

Upvotes: 1

Related Questions