3

I am using Rails 4.1.1, ruby 2.1, mongodb, mongoid as a wrapper, rails_admin for creating admin interfaces

I know that 'attr_accessible' no longer works for Rails4. So i have installed 'protected_attributes' gem. But still no success i am still getting warning in my console

[RailsAdmin] Could not load model Company, assuming model is non existing. (undefined method `attr_accessible' for Company:Class)

So, rails admin do not load the class Company because i have defined attr_accessible in the model. Here is my company model.

  class Company
  include Mongoid::Document

  @@employees_strength = {0 => '0-10', 1 => '11-50', 2 => '51-100', 3 => '101-500', 4 => '501-1000', 5 => '1000+', 6 => '5000+'}

  field :name,          type: String
  field :website,       type: String
  field :domain_name,   type: String
  field :strength,      type: Integer

  has_many :employees
  has_one :admin, :class_name => 'Employee',  :dependent => :destroy, :inverse_of => :organization

  #attr_accessible :name, :website, :domain_name, :strength#, :admin_attributes, :allow_destroy => true
  attr_accessible :admin_attributes
  accepts_nested_attributes_for :admin, :allow_destroy => true
 end

Please any can body can help? Thanks

Stennie
  • 63,885
  • 14
  • 149
  • 175
prashantsahni
  • 2,128
  • 19
  • 20

1 Answers1

2

Mongoid 4 (<= 4.0.2 at the time of writing) does not know about the ActiveModel::MassAssignmentSecurity module provided by protected_attributes gem.

As such you must include the behaviour in your models manually e.g.

class SomeDocument
  include Mongoid::Document
  include ActiveModel::MassAssignmentSecurity

  field :some_field
  attr_accessible :some_field
end

However, this gets tedious pretty quickly so a reasonable alternative is to include the module into the Mongoid::Document module before any of your models are defined.

module Mongoid
  module Document
    include ActiveModel::MassAssignmentSecurity
  end
end
Benjamin Dobell
  • 4,042
  • 1
  • 32
  • 44