Private/protected accessors to attributes in Rails 4 -


supose have following class:

class foo < activerecord::base     belongs_to :bar end 

in rails console can this:

foo = foo.new foo.bar_id = 3 

but can violates encapsulation principle. think better idea do:

foo = foo.new foo.bar = bar.find(3); 

and bar_id should private/protected. has nothing mass assignment , strong parameters security issue too.

is there way set private attributes?

is there way make rails activerecord attributes private?

class mymodel < activerecord::base    private    def my_private_attribute     self[:my_private_attribute]   end    def my_private_attribute=(val)     write_attribute :my_private_attribute, val   end end 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -