Forum: Ruby on Rails Question: coding protected methods

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
620c12639078ea2dde8a3b50ccee4c05?d=identicon&s=25 Dark Ambient (Guest)
on 2006-06-07 17:40
(Received via mailing list)
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
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-06-07 17:52
(Received via mailing list)
On 7-Jun-06, at 7:44 AM, Dark Ambient 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
620c12639078ea2dde8a3b50ccee4c05?d=identicon&s=25 Dark Ambient (Guest)
on 2006-06-07 18:57
(Received via mailing list)
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
D5145c421cd25af6fa577c15219add90?d=identicon&s=25 unknown (Guest)
on 2006-06-07 20:07
(Received via mailing list)
If you don't have it, get Agile Web Development 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
620c12639078ea2dde8a3b50ccee4c05?d=identicon&s=25 Dark Ambient (Guest)
on 2006-06-07 20:54
(Received via mailing list)
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
This topic is locked and can not be replied to.