Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8250433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:53:17+00:00 2026-06-07T23:53:17+00:00

I am working through agile web development with rails 4th edition (rails 3.2+) and

  • 0

I am working through agile web development with rails 4th edition (rails 3.2+) and had a question about migraitons. There is an exercise where I have to add a column to an existing table and then update that new column with values. I need to add a ‘price’ column to ‘line_items’ table. First I generated the migration:

rails generate migration add_price_to_line_items price:decimal

Then I edited the migration file:

class AddPriceToLineItems < ActiveRecord::Migration
  def change
    add_column :line_items, :price, :decimal

    LineItem.all.each do |li|
      li.price = li.product.price
    end
  end

  def down 
    remove_column :line_items, :price
  end
end

Everything worked as planned, however, I had a question about attr_accessible. It is my understanding that all attributes of an object need to be specified in attr_accessible in order to be edited. If not, you usually get this error:

ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: product

As such, all attributes must be set as parameters of attr_accessible in the associated model:

class LineItem < ActiveRecord::Base
  **attr_accessible :cart_id, :product_id, :quantity**
  belongs_to :cart
  belongs_to :product

  def total_price
    product.price * quantity
  end

end

If this is true, then how was my migration able to update the newly generated column? If the column had just been generated, then that new attribute would not yet be specified in the attr_accessible of the associated model. Any and all input would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:53:20+00:00Added an answer on June 7, 2026 at 11:53 pm

    Basicaly it is because in rails forms you can add any field to a form. if a user add a new parameter to the form and submit it to your server it can gives you very big problems.

    Like this:

    your controller:

    LineItem.create(params[:line_item)
    

    if this is your controller the user insert (by javascript or by console editing on chrome) a new textfield he can modify protected fields.

    thats why we use attr_accessible to allow only the defioned fields.

    so the atributes that are not in attr_accessible are still acessible, just not acessible to make a mass assign.

    you still can do things like this:

    model has name, time and date:

    attr_accessible :name, :time
    

    controller or any class:

    You can:

    m = Model.new(:name => "name", :time => "time")
    m.date = "date"
    m.save
    

    You can’t:

    m = Model.new(:name => "name", :time => "time", :date = "date")
    m.save
    

    if you still dont understand check this link http://ruby.railstutorial.org/chapters/modeling-users#sec:accessible_attributes it has a great explanation

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working through Agile Web Development with Rails 4th Edition (Rails 3.2+) and
I am working through the Agile Web Development with Rails book but I have
I'm working through Agile Web Development with Rails, Edition 4 with some tweaks (mostly
Working through Agile Web Development with Rails edition 4 and hit the section on
I'm working through Agile Web Development with Rails and having a problem with Task
I'm working my way through Agile Web Development with Rails and am running into
Just working through the Agile Web Development with Rails book and near the closing
I'm working through Agile Web Dev w/ Rails book (4th ed) and I'm totally
In working through the issues with another question, I've found text files with embedded
I am currently working through Michael Hartl's Rails Tutorial while experimenting with some other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.