John, this is a _fantastic_ point.  It's certainly true that there
would be no way to use the "setter" half of a private attr_accessor in
a regular Ruby object:

self.bar = 'baz'  # violates private
bar = 'baz'        # sets a local variable

But you can use the "getter" half:

return bar.dup   # return a defensive copy of the instance variable

I consider Ruby's definition of "private" fairly crippled, but that's
not the fault of the Rails community, nor can they (we?) do much about
it.

Maybe my actual use case will help clear up why I want a private
attribute.  I have a single-table-inheritance model:

table products:
  integer version
  string name

class Product < ActiveRecord::Base
  attr_private :version  # if such a method existed
end

class VersionedProduct < Foo
  attr_accessor :version  # reveal the method
end

I wouldn't normally ask this list for usage advice, but perhaps this
case will inform design.

Thanks,
Gaius

On Thu, Sep 4, 2008 at 8:50 PM, John D. Hume <[EMAIL PROTECTED]> wrote:
>
>> Also, I'd just like Rails Models to act like normal Ruby Objects.
>
> It would be pretty weird to make attr_accessor methods private in a
> normal Ruby object, since the writer method could never be called
> except with a self.send. If some field was meant to be private, you'd
> probably just use the instance variables directly and not have reader
> or writer methods at all.
>
> It seems like the closest analog in Active Record would be to not
> define reader and writer methods for the attribute, only exposing it
> via read_attribute and write_attribute (and [] and []=).
>
> -hume.
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to