I would like to create read only models that may be the result of some complex join or aggregate query that has little relation to any of the direct table model classes I have defined.
For example the imaginary ActiveRecord::View class
class B < ActiveRecord::View
default_scope do
find_by_sql <<-EOF
select x.alpha alpha, y.a_id a_id from
x.join y on x.id = y.id
EOF
end
belongs_to :a
end
class A < ActiveRecord::Base
has_many :b
end
>> a = A.first
>> puts a.id
10
>> puts a.bs.to_sql
select x.alpha alpha, y.a_id a_id from
x.join y on x.id = y.id
where a_id = 10
I am sure the above or something like it is available but I just can't find it.
B
should be readonly? Did you tried the:readonly
option?Other ways are:
or
I've released a Ruby GEM that solves this problem
https://github.com/bradphelan/Active-Illusion
The spec file verifying the behaviour is