Question: coding protected methods

Apologies first, because I need to ramp up on Ruby and coding Ruby in
Rails,
however it’s my 3rd day with this beast :slight_smile: 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

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

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

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

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