Forum: Ruby on Rails Making AR attriibutes private

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.
75713e3437c6eb18051d7f013bf69b44?d=identicon&s=25 Tim Case (Guest)
on 2006-03-07 18:50
(Received via mailing list)
Hi,

Can anyone tell me the syntax to make AR attributes private so that they
can only be set through an object's constructor?

I want do something like this, where item_id and rated_by are private
and set only through the constructor:


# Schema as of Mon Mar 06 21:22:59 BRT 2006 (schema version 15)
#
#  id                  :integer(11)   not null
#  item_id             :integer(11)   default(0), not null
#  rated_by            :integer(11)   default(0), not null
#  created_on          :datetime      not null
#  updated_on          :datetime      not null
#

class Wow < ActiveRecord::Base
  belongs_to :item
  validates_presence_of :item_id, :rated_by

  def initialize(item, user)
    super()
    self.item_id = item.id
    self.rated_by = user.id
  end

end

How do I make item_id and rated_by private and still accessible
internally by the constructor?

Thanks,

Tim Case
tim@karmacrash.com
This topic is locked and can not be replied to.