Rails: Unknown attribute during model create

Related searches

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.


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 

Comment model:

belongs_to :news

News model:

has_many :comments

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.

unknown attribute: user_id, rails generate migration add_user_id_to_stories you can specify just has_many :stories and in the Story model specify belongs_to :user . With that command we told Rails that we want an Article model, together with a title attribute of type string, and a text attribute of type text. Those attributes are automatically added to the articles table in the database and mapped to the Article model. Rails responded by creating a bunch of files.

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

Getting unknown attribute error when attribute appears to be there , Hello all, I am attempting to create a has_one relationship between 2 models. When Getting unknown attribute error when attribute appears to be there Small script to reduce the image size of Ruby and Ruby on Rails Docker images. In� Active Model BasicsThis guide should provide you with all you need to get started using model classes. Active Model allows for Action Pack helpers to interact with plain Ruby objects. Active Model also helps build custom ORMs for use outside of the Rails framework.After reading this guide, you will know: How an Active Record model behaves. How Callbacks and validations work. How serializers

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

ActiveModel::UnknownAttributeError: unknown attribute Code , ActiveModel::UnknownAttributeError: unknown attribute 'deleted_at' for a value is trying to be set on a copy of a slice from a dataframe when creating new column ActionController::InvalidAuthenticityToken rails when submitting form "make:model" is not defined. command failed with EACCES android� If left public, they can be called from outside of the model and violate the principle of object encapsulation. 3 Available Callbacks. Here is a list with all the available Active Record callbacks, listed in the same order in which they will get called during the respective operations: 3.1 Creating an Object. before_validation; after_validation

ActiveRecord::UnknownAttributeError, Raised when unknown attributes are supplied via mass assignment. class Person include ActiveModel::AttributeAssignment include ActiveModel:: Validations� I wonder if, once all non-db-specific logic (including recent one such as the user-facing casting half) is extracted to AM, it would make sense to have a more full-featured out-of-the-box implementation of active model for those that want it to behave not just as a form object but as much as a full AR as possible, except not persisted.

Active Record Nested Attributes, When you enable nested attributes an attribute writer is defined on the model. Enabling nested attributes on a one-to-one association allows you to create the� If you used the pluralized form in the above example for the author association in the Book model and tried to create the instance by Book.create(authors: @author), you would be told that there was an "uninitialized constant Book::Authors". This is because Rails automatically infers the class name from the association name.

Only attr_accessible attributes will be updated. If your model specified attr_accessible attributes, only those attributes will be updated. Use attr_accessible to prevent mass assignment (by users) of attributes that should not be editable by a user. Mass assignment is used in create and update methods of your standard controller.

  • It could be a issue with Rails' pluralization stuff. The Comment model has a field called new_id ?
  • My comment table has these columns - ID, News_ID, Content
  • add attr_accessible :news_id in the comment model but still don't work
  • That would be my primary guess, as well; we can't be sure, though, because you didn't show your 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
  • it need to be t.integer :news_id ,not t.integer News_ID
  • Thanks, this fix my problem :)
  • When I print with puts params in create action I don't see the hash, I have the error message. Maybe I do something wrong?
  • you doing it in first line of the action... before using the params[:news_id] .. and you need to check the output in rails console (if using webrick server)
  • I am using Aptana studio and I there is nothing in the console
  • can you run rake routes and post the result here... (for comments controller related routes)
  • news_comments GET /news/:news_id/comments(.:format) comments#index POST /news/:news_id/comments(.:format) comments#create new_news_comment GET /news/:news_id/comments/new(.:format) comments#new