Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord::DangerousAttributeError

I have to connect to an existing database used by our freeradius server. One table has a column called attribute which I'm trying to access.

When accessing, I get his error:

ActiveRecord::DangerousAttributeError 
attribute? is defined by ActiveRecord

I've tried to select and rename this column in my model:

def self.default_scope
    Radcheck.select("attribute as newattribute")
end

But that's not working either.

Can anyone recommend a way around this? I'd really like to rename the column in rails!

like image 330
Jenny Blunt Avatar asked Oct 10 '11 21:10

Jenny Blunt


People also ask

What is ActiveRecord :: Base in Rails?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending. Edit: as Mike points out, in this case ActiveRecord is a module... ActiveRecord is defined as a module in Rails, github.com/rails/rails/tree/master/activerecord/lib/…

What is ActiveRecord?

Active Record allows you to validate the state of a model before it gets written into the database. There are several methods that you can use to check your models and validate that an attribute value is not empty, is unique and not already in the database, follows a specific format, and many more.

What is Ruby ActiveRecord?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.


1 Answers

On a similar question I found this answer: https://stackoverflow.com/a/9106597/1266906

Without having to care about which attributes are reserved by ActiveRecord in Rails 3.0 just add

gem 'safe_attributes'

to your Gemfile and the gem will try to take care of all colliding names automatically.

As with the other answer you need to use Radcheck[:attribute] or Radcheck.read_attribute :attribute / Radcheck.write_attribute :attribute, 'value' to access fields with internally reserved names, but the gem ensures validations like validates_presence_of :attribute will work as usual.

Further details are available at https://github.com/bjones/safe_attributes

like image 119
TheConstructor Avatar answered Sep 20 '22 15:09

TheConstructor