Encapsulate Relationships in Rails

It would be really nice if I could encapsulate all of the default accessors that are generated when I create a relationship between two ActiveRecord objects.

For instance, when you declare that `has_and_belongs_to_many :people`, your model is peppered with public accessors for the people collection. These methods are generated for your class at runtime.

I want to provide business logic around those people, and therefore I want to prohibit clients from directly accessing the people collection. I want to provide methods such as `register_person`. Of course, I can add that method, but there's nothing stopping someone from coming along and calling `model.people << person`.

Is there? How to restrict direct access to the collection on an ActiveRecord model?

Popular posts from this blog

Lists and arrays in Dart

Converting Array to List in Scala

Null-aware operators in Dart