bjones / safe_attributes Goto Github PK
View Code? Open in Web Editor NEWAdd support for reserved word column names with ActiveRecord
License: MIT License
Add support for reserved word column names with ActiveRecord
License: MIT License
From google searching it appears someone ran into an issue using safe_attributes and devise together due to another validation somewhere expecting the generated attribute methods to exist. Steps to reproduce were given as use both gems in a project.
While a great idea, this does not help with for example database columns named 'changed' (because a changed? method will be generated, triggering a DangerousAttributeError) or 'reply-to' (because the dash will cause things to explode).
Possibly it needs to hook in deeper, to prevent the creation of any methods at all for these columns.
We have a User class in our Rails project that uses Devise for authentication. The password
and password_confirmation
fields in devise, by default, work a little differently.
In short, those fields conflict with the override of #read_attribute_for_validation.
Rather than focusing on fixing this directly, my vote is that we get rid of requiring the safe attributes railtie by default.
I can work on this if you approve of this solution. Thanks!
ActiveRecord::SubclassNotFound
The single-table inheritance mechanism failed to locate the subclass: 'admin'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite User.inheritance_column to use another column for that information.
Not working.
class User < ActiveRecord::Base
include SafeAttributes::Base
bad_attribute_names :type
scope :admins, -> { where(users: { type: 'admin' }) }
end
Working.
class User < ActiveRecord::Base
self.inheritance_column = :_type_disabled
scope :admins, -> { where(users: { type: 'admin' }) }
end
Env | Version |
---|---|
Ruby | 2.3.0 |
Rails | 4.2.6 |
Hi,
I'm migrating a legacy app from rails 2 - rails 4. I have a column format
in my table which is clashing with instance_method name(and gettin private method format' called for #<Template:0x00000009d9b8c8> error)
. So I used this gem. It was working fine with rails 3 but after moving to rails4(and ruby 2.2.x) its not working. I started getting same issue. Can some one help me in solving the issue please
When using safe_attributes gem I get a 'stack level too deep' error 500 internal server error
The issue seems to be related to rendering with errors.
I have seen the incompatibility issue with Rails4 but I felt it would be good to leave it here anyway.
I think I found 1 case where your gem fails. I can read and update models with an attribute column named "attribute", but I cannot save a new one when the attribute name is "attribute". Other attribute names work fine for all methods.
Try this with Ruby 1.9.2 and Rails 3.1.3
rails g model TestModel
CreateTestModels.rb
class CreateTestModels < ActiveRecord::Migration
def change
create_table :test_models do |t|
t.string :attribute
t.timestamps
end
end
end
TestModel.rb
class TestModel < ActiveRecord::Base
bad_attribute_names :attribute
def blah= value
self[:attribute] = value
end
def blah
self[:attribute]
end
end
rails console
Loading development environment (Rails 3.1.3)
ruby-1.9.2-p290 :001 > t = TestModel.new
=> #<TestModel id: nil, attribute: nil, created_at: nil, updated_at: nil>
ruby-1.9.2-p290 :002 > t.blah = 'blah'
=> "blah"
ruby-1.9.2-p290 :003 > t.save
(512.0ms) BEGIN
SQL (295.2ms) INSERT INTOtest_models
(attribute
,created_at
,updated_at
) VALUES ('blah', '2011-12-09 06:00:21', '2011-12-09 06:00:21')
(305.7ms) ROLLBACK
ArgumentError: wrong number of arguments (0 for 1)
from /Users/kmatthews812/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/attribute_methods/read.rb:141:inattribute' from /Users/kmatthews812/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.1.3/lib/active_model/dirty.rb:143:in
attribute_change'
from /Users/kmatthews812/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.1.3/lib/active_model/dirty.rb:117:in `block in changes'
The strange part is that the data is actually saved to the database. The error is thrown after the data is saved and the rollback never occurs. Any ideas on how to fix this? I have no ability to change the database column name, as this is a legacy database.
Thanks!
Placeholder, after testing against Rails 4 beta1 it is clear the current code does not work with Rails 4. If someone wants to take a stab at fixing it I won't mind the pull request.
I'm looking for a solution to be able to write a column named 'ID' (which isn't the primary key) in an external database.
I noticed in the README it says
Instance methods in ActiveRecord::Base, except for 'id', are combined into a list
Is there any reason why id
is excluded? Is id=
excluded too?
So far my best (only) solution is to monkey patch ActiveRecord::AttributeMethods::Write.write_attribute(attr_name, value)
and then access it using square brackets:
external_object = ExternalObject.new(mapped_attributes_other_than_person_id)
external_object[:id] = person_id
external_object.save
Fantastic plugin - my legacy db has the column 'attribute'
Is it possible to do this - right now I'm getting an error:
NoMethodError: Attempt to call private method `attribute'
S
I have a legacy DB. One of the columns is named attributes. It does cause lots of pain even with bad_attribute_names :attributes
. I was trying to call MyModule::MyModel.new(params_with_attributes), but I get an ArgumentError exception in #assign_attributes
at activerecord (4.2.10) lib/active_record/attribute_assignment.rb
saying When assigning attributes, you must pass a hash as an argument.. Is it the dead end? I'm about to rename the column.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.