Adding a Rails Model with Virtual Attributes at Run Time

I have a Rails model with attributes X, Y, Z

I want to dynamically add P, Q attributes to this model while retrieving data. Attributes P, Q do not need constancy and are intended only for interface manipulations.

Is this even possible? I could achieve this by storing P, Q in another object and manipulating it along with the original model, but thought about asking here what could be a better way.

[rails 2.3.5, ruby ​​1.8.7]

+3
source share
1 answer

Try something similar in your model

def p
  @p || @p = fetch_p
end

your_model.p. fetch_p . .

0

Source: https://habr.com/ru/post/1780316/


All Articles