Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionView::Template::Error (points is defined by ActiveRecord) #34

Closed
rfernand opened this issue Nov 15, 2012 · 15 comments
Closed

ActionView::Template::Error (points is defined by ActiveRecord) #34

rfernand opened this issue Nov 15, 2012 · 15 comments

Comments

@rfernand
Copy link

Before adding the merit gem, I had a model with the points attribute (my Question model).

Now that gives this problem when trying to use a question in a rails form:

ActionView::Template::Error (points is defined by ActiveRecord)

And if I try to create a new Question:

irb(main):005:0* Question.new
ActiveRecord::DangerousAttributeError: points is defined by ActiveRecord
        from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/attribute_methods.rb:38:in `instance_method_already_implemented?'
        from /usr/lib/ruby/gems/1.9.1/gems/activemodel-3.1.3/lib/active_model/attribute_methods.rb:287:in `block in define_attribute_method'
        from /usr/lib/ruby/gems/1.9.1/gems/activemodel-3.1.3/lib/active_model/attribute_methods.rb:286:in `each'
        from /usr/lib/ruby/gems/1.9.1/gems/activemodel-3.1.3/lib/active_model/attribute_methods.rb:286:in `define_attribute_method'
        from /usr/lib/ruby/gems/1.9.1/gems/activemodel-3.1.3/lib/active_model/attribute_methods.rb:282:in `block in define_attribute_methods'
        from /usr/lib/ruby/gems/1.9.1/gems/activemodel-3.1.3/lib/active_model/attribute_methods.rb:282:in `each'
        from /usr/lib/ruby/gems/1.9.1/gems/activemodel-3.1.3/lib/active_model/attribute_methods.rb:282:in `define_attribute_methods'
        from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/attribute_methods.rb:14:in `define_attribute_methods'
        from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/attribute_methods.rb:65:in `respond_to?'
        from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/attribute_methods/read.rb:103:in `read_attribute'
        from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/base.rb:1780:in `attribute_for_inspect'
        from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/base.rb:1897:in `block in inspect'
        from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/base.rb:1895:in `collect'
        from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/base.rb:1895:in `inspect'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'irb(main):006:0> 

Is there a way to avoid this problem without renaming the "points" attribute of my models?

Greetings,

EDIT: Adding the gem 'safe_attributes' to the Gemfile fixes the problem (obtained from http://stackoverflow.com/questions/7718651/activerecorddangerousattributeerror)

EDIT 2: After adding the safe_attributes gem, there is still a problem when trying to save something over the points attribute:

NameError (undefined local variable or method `sash' for #<Question:0x00000003b9b308>):
  app/controllers/admin/games_controller.rb:96:in `block (2 levels) in create'
  app/controllers/admin/games_controller.rb:92:in `each'
  app/controllers/admin/games_controller.rb:92:in `block in create'
  app/controllers/admin/games_controller.rb:69:in `create'

EDIT 3: Adding to the Question model:

  def points
    self[:points]
  end 
  def sash
    self.points
  end 

Seems to fix it. I will continue testing this, maybe more problems can arise from this.

@tute
Copy link
Member

tute commented Nov 15, 2012

There's a recent commit in a fork which renames points to sum_points inside of model_additions.rb in jruizes@1f7ceee.

I'll take a look at what's happening, and if it's better I'll rename it to total_points.

Thanks for your feedback!

@tute
Copy link
Member

tute commented Nov 15, 2012

Meanwhile you may monkey patch ActiveRecord so you can work: http://stackoverflow.com/a/1515571/356060 (valid is points in this case).

@rfernand
Copy link
Author

I also tried with the fix for DangerousAtrributeError from the UPDATE notice[1], but did not work for me (the monkey patch method). The environment is Rails 3.1.3.

[1]: If you get an ActiveRecord::DangerousAttributeError: points exception, you may need to temporarily tweak your meritable model, as explained in http://stackoverflow.com/a/1515571/356060.

Thanks for your quick response.
I will try with the fork you mentioned!

@tute
Copy link
Member

tute commented Nov 15, 2012

More: I'll rename the method name as it may collide with ActiveRecord.

Anyway you may need to change that attribute name in your application: http://stackoverflow.com/questions/7718651/activerecorddangerousattributeerror

@tute
Copy link
Member

tute commented Nov 15, 2012

Pretty sure it's coming from your Question model, monkey patch that one also, and if it works then rename the attribute.

@rfernand
Copy link
Author

I tried the gem 'safe_attributes' from http://stackoverflow.com/questions/7718651/activerecorddangerousattributeerror and it fixed the problem!

Thanks for your lighting-fast responses!

@tute
Copy link
Member

tute commented Nov 15, 2012

Happy it works for now! :-) But there is a design flaw inside of merit.

Rails raises ActiveRecord::DangerousAttributeError if it sees a method on your model is already defined in ActiveRecord::Base. But merit is adding many methods to AR::Base, one being points. So not only is merit polluting AR::Base with specific methods, but also It's not allowing client apps to have that common attribute name.

I'll leave this issue open until I rework that. Thanks for the feedback!

@rfernand
Copy link
Author

Understood! Thanks for the great work!

@rfernand
Copy link
Author

Bummer! After adding the safe_attributes gem, there is still a problem when trying to save something over the points attribute:

NameError (undefined local variable or method `sash' for #<Question:0x00000003b9b308>):
  app/controllers/admin/games_controller.rb:96:in `block (2 levels) in create'
  app/controllers/admin/games_controller.rb:92:in `each'
  app/controllers/admin/games_controller.rb:92:in `block in create'
  app/controllers/admin/games_controller.rb:69:in `create'

Any idea how to hack the create method to avoid this error?

EDIT: Adding to the Question model:

  def points
    self[:points]
  end 
  def sash
    self.points
  end 

Seems to fix it. I will continue testing this, maybe more problems can arise from this.

@tute
Copy link
Member

tute commented Nov 15, 2012

Why does a Question need a sash? Is it "meritable"? If not, please paste your rules in a gist.
Anyway sash should be invisible to the app.

@rfernand
Copy link
Author

It is not "meritable", but somehow when calling Question.points in the create method it asked for it.

@tute
Copy link
Member

tute commented Nov 15, 2012

There's the method names collision, the reason for the exception.

Merit needs to add the points method only to meritable models instead of to AR::Base. Relevant code in https://github.com/tute/merit/blob/master/lib/merit/model_additions.rb#L26-52

Maybe I can review it tomorrow or this weekend.

@tute
Copy link
Member

tute commented Nov 23, 2012

Hi @rfernand! Apparently latest commit (4347347) fixes the issue. Could you please try with git's version? If it works fine, I'll release a new minor version.
Thanks!

@rfernand
Copy link
Author

Ok, tested again with the git version of merit and it seems to work fine now.

I removed the code from Question that I had to add before:

#  def points
#    self[:points]
#  end 
#  def sash
#    self.points
#  end 

And tested create, new, save, question.points. No more annoying problems were found!

Also tested user.points, adding badges/points to user, all with no problems.

Thanks!

@tute tute closed this as completed Nov 24, 2012
@tute
Copy link
Member

tute commented Nov 24, 2012

Awesome, you may use version 1.1.0 now.
Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants