Hello,
Is it possible to initialize model attributes from a SQL select
element that’s not backed by a real database column?
For example:
class A << ActiveRecord::Base
attr_accessor :state_info #Not a db column
has_many :b, :finder_sql=‘SELECT b.*, ‘#{state_info}’ as
state_info FROM b WHERE b.a_id = #{id}’
end
class B << ActiveRecord::Base
attr_accessor :state_info #Not a db column
belongs_to :a
end
a.state_info = “test”
b = a.b.first
#b.state_info is nil, but I want it to be initialized to “test” when
it’s returned from the :finder_sql.
I don’t think I can accomplish this with an after_find/
after_initialize callback either, since I need to have access to the
value of a.state_info in order to set b.state_info.
Thanks,
Tom