# Can I initialize model attributes from SQL?

**URL:** <https://discuss.rubyonrails.org/t/can-i-initialize-model-attributes-from-sql/21124>\
**Category:** rubyonrails-talk\
**Created:** [December 2, 2007, 4:18am UTC](https://discuss.rubyonrails.org/t/can-i-initialize-model-attributes-from-sql/21124 "2007-12-02T04:18:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom\_302](https://avatars.discourse-cdn.com/v4/letter/t/e480ec/32.png) [@tom\_302](https://discuss.rubyonrails.org/u/tom_302)\
**Post date:** [December 2, 2007, 4:18am UTC](https://discuss.rubyonrails.org/t/can-i-initialize-model-attributes-from-sql/21124/1 "2007-12-02T04:18:46Z")

</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 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :state\_info #Not a db column &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;has\_many :b, :finder\_sql='SELECT b.\*, \'#{state\_info}\' as state\_info FROM b WHERE b.a\_id = #{id}' end

class B \<\< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :state\_info #Not a db column &nbsp;&nbsp;&nbsp;&nbsp;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:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [December 2, 2007, 10:49am UTC](https://discuss.rubyonrails.org/t/can-i-initialize-model-attributes-from-sql/21124/2 "2007-12-02T10:49: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 &nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :state\_info #Not a db column &nbsp;&nbsp;&nbsp;&nbsp;has\_many :b, :finder\_sql='SELECT b.\*, \'#{state\_info}\' as state\_info FROM b WHERE b.a\_id = #{id}' end
> 
> class B \<\< ActiveRecord::Base &nbsp;&nbsp;&nbsp;attr\_accessor :state\_info #Not a db column &nbsp;&nbsp;&nbsp;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'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://avatars.discourse-cdn.com/v4/letter/t/e480ec/32.png) [@tom\_302](https://discuss.rubyonrails.org/u/tom_302)\
**Post date:** [December 2, 2007, 9:00pm UTC](https://discuss.rubyonrails.org/t/can-i-initialize-model-attributes-from-sql/21124/3 "2007-12-02T21:00:16Z")

</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?
