i have a model say modela with a calculate field. like below
class modelA < ActiveRecord::Base
attr_accessor : calc_field
def calc_field
@stuff = modelb.all
//do fancy things with stuff
end
end
now my problem is if i have 100 records of modelA. the modelb.all gets called a 100times. but its the same dataset each time. but the query gets sent out a 100 times.
is there anyway/anywhere i can declare @stuff globally so its gets shared across all instances of modelA. so it gets called only once.
There are numerous ways on how to tackle this problem.
Solution A:
And then in your code work flow
Solution B