Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 434
user1107922 Avatar asked Sep 21 '12 18:09

user1107922


3 Answers

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.

like image 127
maximus ツ Avatar answered Nov 07 '22 12:11

maximus ツ


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..

like image 1
Rahul garg Avatar answered Nov 07 '22 12:11

Rahul garg


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

like image 1
Promise Preston Avatar answered Nov 07 '22 14:11

Promise Preston