Any interest in "sudo" methods for bypassing mass-assignment?

68 views
Skip to first unread message

Peter Brown

unread,
Aug 12, 2012, 10:32:29 PM8/12/12
to rubyonra...@googlegroups.com
I wrote a gem a while ago that adds sudo_* methods to ActiveRecord models to bypass mass-assignment protection, and I was curious if there would be any interest in adding similar functionality to Rails. I find it really useful when you want to quickly create a few records in the console, but can't remember the syntax for "without_protection" or which role can update which attributes. Other potential uses could be for seed data and tests.

Here are a few examples of how you might use it:
# Given a User class
class User < ActiveRecord::Base
  attr_accessible :name
end
# Creating a new user
> User.sudo_create(name: 'Pete', email: 'em...@example.com', account: Account.first)
# Updating an existing user
> new_account = Account.last
> User.find(1).sudo_update_attributes(account: new_account)

Godfrey Chan

unread,
Aug 12, 2012, 10:39:43 PM8/12/12
to rubyonra...@googlegroups.com
but can't remember the syntax for "without_protection"

At the risk of asking the obvious question, what exactly is so confusing about..
User.create({name: 'Pete', email: 'em...@example.com', account: Account.first}, without_protection: true)
and…
User.find(1).update_attributes({account: new_account}, without_protection: true)
?

Maybe it's just me, but the differences between this and sudo_* seems so minimal that I don't think it's worth it.

Godfrey

Rafael Mendonça França

unread,
Aug 12, 2012, 10:45:53 PM8/12/12
to rubyonra...@googlegroups.com
We are going to remove mass-assignment protection in the model layer from the core so I think we are not interested.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-co...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.

Peter Brown

unread,
Aug 12, 2012, 11:04:52 PM8/12/12
to rubyonra...@googlegroups.com
Rafael, 

That's good to know, thanks!

Steve Klabnik

unread,
Aug 12, 2012, 11:27:37 PM8/12/12
to rubyonra...@googlegroups.com
> That's good to know, thanks!

https://github.com/rails/strong_parameters for more deets.
Reply all
Reply to author
Forward
0 new messages