# Can I initialize model attributes from SQL?

**URL:** <https://www.ruby-forum.com/t/can-i-initialize-model-attributes-from-sql/121868>\
**Category:** Rails\
**Created:** [December 2, 2007, 5:19am UTC](https://www.ruby-forum.com/t/can-i-initialize-model-attributes-from-sql/121868 "2007-12-02T05:19:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom\_302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e480ec/32.png) [@tom\_302](https://www.ruby-forum.com/u/tom_302)\
**Post date:** [December 2, 2007, 5:19am UTC](https://www.ruby-forum.com/t/can-i-initialize-model-attributes-from-sql/121868/1 "2007-12-02T05:19:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tom\_302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e480ec/32.png) [@tom\_302](https://www.ruby-forum.com/u/tom_302)\
**Post date:** [December 2, 2007, 11:50am UTC](https://www.ruby-forum.com/t/can-i-initialize-model-attributes-from-sql/121868/2 "2007-12-02T11:50:23Z")

</div>

On 2 Dec 2007, at 04:18, tom\_302 wrote:

> ```
> has_many :b, :finder_sql='SELECT b.*, \'#{state_info}\' as
> 
> ```
> 
> #b.state\_info is nil, but I want it to be initialized to “test” when  
> it’s returned from the :finder\_sql.

I’m assuming that :finder\_sql=’ is a typo for :finder\_sql=\>’  
In activerecord any column in the sql result ends up being accessible.  
In this case the attr\_accessor you’ve got on B is actually clobbering  
the state\_info column.

Fred

---

<div class="post-metadata">

**Author:** ![tom\_302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e480ec/32.png) [@tom\_302](https://www.ruby-forum.com/u/tom_302)\
**Post date:** [December 2, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/can-i-initialize-model-attributes-from-sql/121868/3 "2007-12-02T22:00:36Z")

</div>

Thanks Fred,

The problem is that I need a transient variable on the models (e.g.  
attr\_accessor :state\_info). There are other models, A, B, C, D,…  
chained together by foreign key, and transient :state\_info. I need to  
use the value of :state\_info as criteria in the finder\_sql in each  
link of the chain. So, the chain is traversed differently depending  
on how the root object’s state\_info is initialized.

Is there any way to keep the attr\_accessor on the models and also load  
the select-element into it with finder\_sql?

On Dec 2, 5:49 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
