I am accessing a database that I can't change and it has a column named valid defined. Anytime I try to access an attribute, I get this exception:
valid? is defined by ActiveRecord (ActiveRecord::DangerousAttributeError)
The exception makes sense, but since I'm not able to change the database, how can I get around this error?
I tried "overriding" the attribute, but I don't know how to remove the original column. I can successfully call this valid_column method, but any time I try to access another attribute defined in the database, I get the same exception. It still seems to be trying to map the valid column.
def valid_column=(valid)
write_attribute(:valid, valid)
end
def valid_column
read_attribute(:valid)
end
I'm not sure if it matters, but here are the details of my environment:
Thanks in advance!
ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.
Active Record uses naming conventions for the columns in database tables, depending on the purpose of these columns. Foreign keys - These fields should be named following the pattern singularized_table_name_id (e.g., item_id , order_id ).
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.
Try this:
class MyTable < AR:Base
class << self
def instance_method_already_implemented?(method_name)
return true if method_name == 'valid'
super
end
end
end
It's a hack, and it might not work in rails 3, but it could fix the problem for now.
I found it on the ruby on rails mailing list
If you wanted, you could also look at datamapper, which handles these sort of things somewhat more sanely.
Use safe_attributes - https://github.com/bjones/safe_attributes . It works perfectly out of the box:
class WebsiteUser < ActiveRecord::Base
establish_connection 'cf_website'
set_table_name 'nc_users'
bad_attribute_names :hash
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With