# Hardcode database record reference into model

**URL:** <https://www.ruby-forum.com/t/hardcode-database-record-reference-into-model/167363>\
**Category:** Rails\
**Created:** [May 7, 2009, 1:55am UTC](https://www.ruby-forum.com/t/hardcode-database-record-reference-into-model/167363 "2009-05-07T01:55:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![reynar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@reynar](https://www.ruby-forum.com/u/reynar)\
**Post date:** [May 7, 2009, 1:55am UTC](https://www.ruby-forum.com/t/hardcode-database-record-reference-into-model/167363/1 "2009-05-07T01:55:38Z")

</div>

Hi,

I have a table containing system account types (“Income”, “Ordered”,  
“Available” etc) which are created as seed data during application  
installation.

class InventoryAccountType \< ActiveRecord::Base

has\_many :inventory\_accounts  
enumerable\_constant :normal\_balance, :constants =\> [:debit, :credit]

validates\_uniqueness\_of :name

# system account types

def self.income  
find\_by\_name(“Income”)  
end

def self.ordered  
find\_by\_name(“Ordered”)  
end

def self.available  
find\_by\_name(“Available”)  
end

end

These account types represent system concepts that are referenced in  
code elsewhere in application, for example:

class InventoryTransaction \< ActiveRecord::Base

has\_many :inventory\_account\_entries  
validate :account\_entries\_balance

# locate new inventory

def self.create\_inventory(product\_sku, storage\_location, quantity,  
inventory\_batch = nil)

```
# if no batch specified, create a new batch
if inventory_batch.nil?
  inventory_batch = InventoryBatch.create(:product_sku =>

```

product\_sku)  
end

```
product_income_account = InventoryAccount.retrieve_by_type

```

(product\_sku, InventoryAccountType.income)  
available\_product\_location\_account =  
InventoryAccount.retrieve\_by\_location(inventory\_batch,  
storage\_location, InventoryAccountType.available)

```
account_transfer(product_income_account,

```

available\_product\_location\_account, quantity)  
end  
end

Do you think this approach to hardcoding record lookups into the  
InventoryAccountType model in such a way is good practice?

There may be other InventoryAccountTypes created by the user during  
application usage but these would be only be managed and allocated to  
by the user as general account transfers. Not by system core system  
use cases such as creating new inventory as addressed by it own  
methods in the model.

I have also added a “system” boolean column to the  
InventoryAccountType table so as to know which types cannot be deleted  
or edited.

I was also thinking of replacing the:

def self.available  
find\_by\_name(“Available”)  
end

…style methods for each system type with some of dynamic ruby method  
that automatically creates a method for any database record that has  
system == 1. This would save having to clutter the  
InventoryAccountType model with multiple method definitions doing the  
same thing.

Thanks in advance, Andrew.

---

<div class="post-metadata">

**Author:** ![reynar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@reynar](https://www.ruby-forum.com/u/reynar)\
**Post date:** [May 7, 2009, 2:09am UTC](https://www.ruby-forum.com/t/hardcode-database-record-reference-into-model/167363/2 "2009-05-07T02:09:06Z")

</div>

hi,

I think you should use named\_scope instead of this kind of access…

class InventoryAccountType \< ActiveRecord::Base  
named\_scope :income, :conditions =\> {:name =\> ‘Income’}  
named\_scope :ordered, :conditions =\> {:name =\> ‘Ordered’}  
end

you can use it the same way but you can chain scopes… eg

data = InventoryAccountType.income.ordered

in your case (with def.self methods) is that not possible

tom

Andrew E. wrote:

> end  
> class InventoryTransaction \< ActiveRecord::Base  
> inventory\_batch = InventoryBatch.create(:product\_sku =\>  
> available\_product\_location\_account, quantity)  
> methods in the model.
> 
> …style methods for each system type with some of dynamic ruby method  
> that automatically creates a method for any database record that has  
> system == 1. This would save having to clutter the  
> InventoryAccountType model with multiple method definitions doing the  
> same thing.
> 
> Thanks in advance, Andrew.

# –

Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

# [www.meinlschmidt.com](http://www.meinlschmidt.com) www.maxwellrender.cz www.lightgems.cz

---

<div class="post-metadata">

**Author:** ![reynar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@reynar](https://www.ruby-forum.com/u/reynar)\
**Post date:** [May 7, 2009, 3:10am UTC](https://www.ruby-forum.com/t/hardcode-database-record-reference-into-model/167363/3 "2009-05-07T03:10:33Z")

</div>

On Thursday 07 May 2009, Andrew E. wrote:

> I have a table containing system account types (“Income”, “Ordered”,  
> “Available” etc) which are created as seed data during application  
> installation.

The never-ending enumeration story. For my take on it see (without line  
break, with hyphen):

[http://www.schuerig.de/michael/blog/index.php/2009/04/02/simplistic-](http://www.schuerig.de/michael/blog/index.php/2009/04/02/simplistic-)  
enums/

I notice that my caching mechanism would get in the way of your  
requirement to have “non-managed” enumeration values.

> These account types represent system concepts that are referenced in  
> code elsewhere in application, for example:
> 
> class InventoryTransaction \< ActiveRecord::Base  
> [snip]

With the enums implementation linked above, I’m handling a similar case  
like this:

class InventoryAccountType \< ActiveRecord::Base  
has\_many :inventory\_accounts

enumerates do |e|  
e.value :name =\> ‘income’, :title =\> ‘Income’  
e.value :name =\> ‘ordered’, :title =\> ‘Ordered’  
e.value :name =\> ‘available’, :title =\> ‘Available’  
end  
end

class InventoryAccount \< ActiveRecord::Base  
belongs\_to :inventory\_account\_type

InventoryAccountType.each\_name do |name|  
named\_scope “of\_#{name}\_type”,  
:joins =\> :inventory\_account\_type,  
:conditions =\> { :inventory\_account\_types =\> { :name =\> name } }  
end  
end

Then you can do thinks like

InventoryAccount.of\_income\_type.find(…)

Without the trickery, you could define a parameterized scope like this

class InventoryAccount \< ActiveRecord::Base  
belongs\_to :inventory\_account\_type

named\_scope :of\_type, lambda { |type|  
:joins =\> :inventory\_account\_type,  
:conditions =\> { :inventory\_account\_types =\> { :name =\> type } }  
}  
end

And use it as

InventoryAccount.of\_type(:income).find(…)

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![reynar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@reynar](https://www.ruby-forum.com/u/reynar)\
**Post date:** [May 7, 2009, 3:11am UTC](https://www.ruby-forum.com/t/hardcode-database-record-reference-into-model/167363/4 "2009-05-07T03:11:45Z")

</div>

Thanks Tom,

I’ve just tried using method\_missing to generate the methods, but  
thinking about it your named\_scope suggestion might have future value,  
chaining etc. Also, I think the named\_scope might be a bit more self  
documenting and helps enforce nothing gets missed or forgotten about.

# dynamically create lookup method for any system account type

def self.method\_missing(method)  
if account\_type = find(:first, :conditions =\> { :name =\>  
method.to\_s, :system\_type =\> true })  
account\_type  
else  
super  
end  
end

Thanks, Andrew.
