Rails Create method resulting in Nil records

左心房为你撑大大i 提交于 2020-07-09 04:16:59

问题


Why does this create method make a nil record?

I have tried:

Dropdown.create(subject: "test")

Dropdown.create({subject: "test", subject_value: "1"})

Dropdown.create({:subject => "test", :subject_value => "1"})

All result in nil records.

   (0.1ms)  begin transaction
  SQL (0.6ms)  INSERT INTO "dropdowns" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2016-01-21 23:58:04.979225"], ["updated_at", "2016-01-21 23:58:04.979225"]]
   (2.1ms)  commit transaction
=> #<Dropdown id: 145, subject: nil, visible: nil, subject_value: nil, subject_description: nil, created_at: "2016-01-21 23:58:04", updated_at: "2016-01-21 23:58:04

Model file

class Dropdown < ActiveRecord::Base
  
    FIELDS =  [
              :subject,
              :visible,
              :subject_value,
              :subject_description
            ]
  
  attr_accessor(*FIELDS)
  subjects = %w[math english spanish]
  
  subjects.each do |s|
    scope s.to_sym, -> { where(subject: s) }
  end

end

Migration file

class CreateDropdowns < ActiveRecord::Migration
  def change
    create_table :dropdowns do |t|
      t.string :subject
      t.boolean :visible
      t.string :subject_value
      t.string :subject_description

      t.timestamps null: false
    end
  end
end

回答1:


You're overriding your ActiveRecord::Base functionality by declaring attr_accessor:

#app/models/dropdown.rb
class Dropdown < ActiveRecord::Base

  subjects = %w(math english spanish)

  subjects.each do |s|
    scope s.to_sym, -> { where(subject: s) }
  end

end
  1. You don't need to declare FIELDS -- you can call @model.attributes (instance) or Model.column_names (class) to get all the fields for that model.

  2. attr_accessor creates a set of getter/setter methods in the class. This overrides any attributes you may have from your db, which is why you're getting nil entries when you save. Good ref here.

--

The above model should work for you.




回答2:


It is not necessary to use attr_accessor if these fields are columns of a table in your database. ActiveRecord done it for you.

Try to use attr_accessible instead of attr_accessor. But for rails 4+, not necessary to use it. see http://apidock.com/rails/ActiveModel/MassAssignmentSecurity/ClassMethods/attr_accessible

This method is deprecated or moved on the latest stable version. The last existing version (v3.2.13) is shown here.



来源:https://stackoverflow.com/questions/34937935/rails-create-method-resulting-in-nil-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!