# Question: coding protected methods

**URL:** <https://www.ruby-forum.com/t/question-coding-protected-methods/95624>\
**Category:** Rails\
**Created:** [June 7, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/question-coding-protected-methods/95624 "2006-06-07T17:40:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dark\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@Dark\_A](https://www.ruby-forum.com/u/Dark_A)\
**Post date:** [June 7, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/question-coding-protected-methods/95624/1 "2006-06-07T17:40:17Z")

</div>

Apologies first, because I need to ramp up on Ruby and coding Ruby in  
Rails,  
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:

protected  
method…  
end

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” ?

TIA  
Stuart

---

<div class="post-metadata">

**Author:** ![Dark\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@Dark\_A](https://www.ruby-forum.com/u/Dark_A)\
**Post date:** [June 7, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/question-coding-protected-methods/95624/2 "2006-06-07T17:52:43Z")

</div>

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=  
end

x = Foo.new  
x.column1 = “hello” # disallowed  
x.attributes = {:column1 =\> ‘hello’} # allowed  
Foo.new(:column1 =\> ‘hello’) # allowed

class Bar \< ActiveRecord::Base  
attr\_protected :column1=  
end

x = Bar.new(:column1 =\> ‘hello’) # column1 parameter silently discarded  
x.attributes = {:column1 =\> ‘hello’} # silently discarded here too  
x.column1 = ‘hello’ # allowed

Regards,  
Trevor

---

<div class="post-metadata">

**Author:** ![Dark\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@Dark\_A](https://www.ruby-forum.com/u/Dark_A)\
**Post date:** [June 7, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/question-coding-protected-methods/95624/3 "2006-06-07T20:07:26Z")

</div>

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.  
-Nathan

---

<div class="post-metadata">

**Author:** ![Dark\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@Dark\_A](https://www.ruby-forum.com/u/Dark_A)\
**Post date:** [June 7, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/question-coding-protected-methods/95624/4 "2006-06-07T18:57:08Z")

</div>

Trevor,  
Thanks for the very detailed explanation. I don’t think it’s totally  
sunk  
in but I"ll continue to re-read and look up more info on protected  
methods.  
Probably best if I combine learning Rails with Ruby as well.

Stuart

---

<div class="post-metadata">

**Author:** ![Dark\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@Dark\_A](https://www.ruby-forum.com/u/Dark_A)\
**Post date:** [June 7, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/question-coding-protected-methods/95624/5 "2006-06-07T20:54:20Z")

</div>

Nathan

Thanks for the suggestion - I was actually considering asking. Reason  
being  
that I have Agile Web Dev (2nd edition now) and after reading the  
prelimanary overview of rails, went ahead and started on the depot.  
However  
it seemed to me (and it maybe premature of me having not read through  
the  
entire book) that a good Ruby compliment would be beneficial. So I  
picked  
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  
copying  
code without really understanding what’s being written. This is  
something I  
would prefer to avoid (unless it paid the rent :)).

Stuart
