--
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.
--
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.
Shouldn't that be handled with validations? I mean you wouldn't save the model without having a validation to ensure the value is consistent with the state anyways, so why not just take whatever value and let it blow up on validation?
On Monday, October 1, 2012 11:00:07 PM UTC+2, Godfrey Chan wrote:Shouldn't that be handled with validations? I mean you wouldn't save the model without having a validation to ensure the value is consistent with the state anyways, so why not just take whatever value and let it blow up on validation?
It feels wrong to use validations for this. Then again, if Rails 4 moves away from attr_accessible towards strong_parameters (which essentially shifts mass assignment protection from the model to the controller), validations will soon be the only way to protect attributes in the model.
As a side note: I'm not sure I like the way this is going in Rails 4. The weblog reads: "We should never have put mass-assignment protection into the model, and many people stopped doing so long ago." (http://weblog.rubyonrails.org/2012/3/21/strong-parameters/) Why not? It seems perfectly natural to me that a model exposes different attributes under certain circumstances.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/IYq-qooSH8YJ.
It feels wrong to use validations for this. Then again, if Rails 4 moves away from attr_accessible towards strong_parameters (which essentially shifts mass assignment protection from the model to the controller), validations will soon be the only way to protect attributes in the model.Why? I think it might be that you are using "attr_accessible" incorrectly. "attr_accessible" and "attr_protected" are unfortunately named. They doesn't protect your models in anyway EXCEPT when using mass assignment. Even with "attr_accessible :field", you can still access model.field and set it to whatever value without any error. It does not provide any level of guarantee that I'd consider "protection" for your models. You definitely want a validation rule for this.
As a side note: I'm not sure I like the way this is going in Rails 4. The weblog reads: "We should never have put mass-assignment protection into the model, and many people stopped doing so long ago." (http://weblog.rubyonrails.org/2012/3/21/strong-parameters/) Why not? It seems perfectly natural to me that a model exposes different attributes under certain circumstances.Mass assignment itself is perfectly safe. That alone is not the issue. Mass assignment with *untrusted data* is the problem. And sanitizing user input sounds like a controller (or wherever that data is coming from) concern to me.
And maybe the gem even makes it into Rails 4.