Forum: Ruby on Rails Question: coding protected methods

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Dark A. (Guest)
on 2006-06-07 19:40
(Received via mailing list)
Apologies first, because I need to ramp up on Ruby and coding Ruby in
however it's my 3rd day with this beast :) so I'm asking :

When I added protected methods to the model before it was like:


Would this be a valid way to write a protected method as well ?:

attr_protected :column1, :column2

Perhaps this particular call is already protected inherent to the
call, yet do I need to
surround it with "protected" and "end" ?

Trevor S. (Guest)
on 2006-06-07 19:52
(Received via mailing list)
On 7-Jun-06, at 7:44 AM, Dark A. wrote:

> attr_protected :column1, :column2
'protected :foo' is not the same as 'attr_protected :foo'

'protected' affects the ability of other objects to call the 'foo'
method on your object.

'attr_protected' affects the params that are considered acceptable
for mass assignment (new({}), attributes={}, update_attributes({}))
for an ActiveRecord::Base subclass (i.e. your models).

For example:

class Foo < ActiveRecord::Base
   protected :column1=

x =
x.column1 = "hello" # disallowed
x.attributes = {:column1 => 'hello'} # allowed => 'hello') # allowed

class Bar < ActiveRecord::Base
   attr_protected :column1=

x = => 'hello') # column1 parameter silently discarded
x.attributes = {:column1 => 'hello'} # silently discarded here too
x.column1 = 'hello' # allowed

Dark A. (Guest)
on 2006-06-07 20:57
(Received via mailing list)
Thanks for the very detailed explanation.  I don't think it's totally
in but I"ll continue to re-read and look up more info on protected
Probably best if I combine learning Rails with Ruby as well.

unknown (Guest)
on 2006-06-07 22:07
(Received via mailing list)
If you don't have it, get Agile Web D. with Rails and work
through that. That's a good way of getting going pretty quickly. When
I first started (only a few weeks ago), I found it just fine to learn
rails and pick up the ruby as I went along.
Dark A. (Guest)
on 2006-06-07 22:54
(Received via mailing list)

Thanks for the suggestion - I was actually considering asking.  Reason
that I have Agile Web Dev (2nd edition now) and after reading the
prelimanary overview of rails, went ahead and started on the depot.
it seemed to me (and it maybe premature of me having not read through
entire book) that a good Ruby compliment would be beneficial.  So I
up the Manning book, Ruby for Rails by David Black.  The title makes the
contents self-descriptive.
Curious as to what others might think because 2 books at this point
might be
overload for me, yet I don't want to plow through rails without a better
understanding of what I need to know in Ruby.  I read somewhere recently
that people come to rails without understanding ruby and develop by
code without really understanding what's being written.  This is
something I
would prefer to avoid (unless it paid the rent :)).

This topic is locked and can not be replied to.