Cannot instance object with attr_accessible in Rails

I have created a Category class:

class Category < ActiveRecord::Base
    attr_accessible :name
    has_many :posts
end

When I created a new object:

category = Category.new(:name => "News")

I am getting this error:

`NoMethodError: undefined method 'attr_accessible' for Category(call 
'Category.connection' to establish a connection):Class  ...

How can I resolve this?

Upvotes: 2

Views: 700

Answers (4)

Aziz Zoaib
Aziz Zoaib

Reputation: 771

Below should fix also. No need to put ::Base -- you can remove it.

class Category < ActiveRecord
  attr_accessor :name
  has_many :posts
end

Upvotes: 0

Samsad CV
Samsad CV

Reputation: 125

Use attr_accessor there.. it will help you to solve the issue

Upvotes: 0

Ali Raza
Ali Raza

Reputation: 943

This will fix your issue.

class Category < ActiveRecord::Base

    def user_params
      params.require(:name)
    end
    has_many :posts
end

Upvotes: 1

infused
infused

Reputation: 24337

You want to use attr_accessor, not attr_accessible.

attr_accessor is a Ruby method that defines setter and getter methods, while attr_accessible lets you whitelist ActiveRecord attributes for mass assignment.

Upvotes: 0

Related Questions