简体   繁体   中英

Can't assign string column on ActiveRecord model

Given the following stage_item.rb :

class StageItem < ActiveRecord::Base
  attr_accessor :headers, :line_attributes
end

And schema.rb :

create_table "stage_items", force: :cascade do |t|
  t.integer  "item_id"
  t.boolean  "valid_item_id"
  t.boolean  "ok_to_clearance"
  t.string   "headers"
  t.string   "line_attributes"
  t.string   "error"
  t.datetime "created_at",      null: false
  t.datetime "updated_at",      null: false
end

And inside the StageItemsController :

class StageItemsController < ApplicationController
  def new
  end

  def create
    stage_item = StageItem.new
    require 'pry-byebug'
    binding.pry
    redirect_to root_url
  end
end

Why does stage_item.item_id = 42 work but not stage_item.headers = "foobar" ?

[1] pry(#<StageItemsController>)> stage_item
=> #<StageItem id: nil, item_id: nil, valid_item_id: nil, ok_to_clearance: nil, headers: nil, line_attributes: nil, error: nil, created_at: nil, updated_at: nil>
[2] pry(#<StageItemsController>)> stage_item.headers = "foobar"
=> "foobar"
[3] pry(#<StageItemsController>)> stage_item
=> #<StageItem id: nil, item_id: nil, valid_item_id: nil, ok_to_clearance: nil, headers: nil, line_attributes: nil, error: nil, created_at: nil, updated_at: nil>
[4] pry(#<StageItemsController>)> stage_item.item_id = 42
=> 42
[5] pry(#<StageItemsController>)> stage_item
=> #<StageItem id: nil, item_id: 42, valid_item_id: nil, ok_to_clearance: nil, headers: nil, line_attributes: nil, error: nil, created_at: nil, updated_at: nil>

You don't need that line since you have stage_items table

attr_accessor :headers, :line_attributes

I believe it messes up things for you

Please remove :headers from attr_accessor since column "headers" is already attr_accessible.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM