# Dynamic models and activerecord

**URL:** <https://www.ruby-forum.com/t/dynamic-models-and-activerecord/120293>\
**Category:** Rails\
**Created:** [November 15, 2007, 4:14pm UTC](https://www.ruby-forum.com/t/dynamic-models-and-activerecord/120293 "2007-11-15T16:14:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![minkymorgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ec9cab/32.png) [@minkymorgan](https://www.ruby-forum.com/u/minkymorgan)\
**Post date:** [November 15, 2007, 4:14pm UTC](https://www.ruby-forum.com/t/dynamic-models-and-activerecord/120293/1 "2007-11-15T16:14:09Z")

</div>

Hi,

I’m kinda new to rails, could someone point me in the right direction?

I have a load of model names sitting in a table. Given a loop through  
those Model names, how do I call activerecord functions, like count,  
on each iteration dynamically to display summary info in each link?

In my activescaffold helper I have the following code that generates  
an error:

def tag\_pivot\_column(record)  
model = record.cube.gsub(/cube\_/, ‘tag\_pivot\_’)  
model = model.singularize

# error here. How do I call activerecord given my actual model name

is in the variable called “model”?  
num = model.count.to\_s

# I am testing the line below, that shows the number of records in

the link text  
link\_to(model + " [" + num + "] ", “/” \<\< record.cube.gsub(/cube\_/,  
‘tag\_pivot\_’))

```
# original working line of code.
# link_to(record.cube.gsub(/cube_/, 'tag_pivot_'), "/" <<

```

record.cube.gsub(/cube\_/, ‘tag\_pivot\_’))

end

---

<div class="post-metadata">

**Author:** ![minkymorgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ec9cab/32.png) [@minkymorgan](https://www.ruby-forum.com/u/minkymorgan)\
**Post date:** [November 15, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/dynamic-models-and-activerecord/120293/2 "2007-11-15T17:02:45Z")

</div>

On 15 Nov 2007, at 15:13, minkymorgan wrote:

> an error:
> 
> def tag\_pivot\_column(record)  
> model = record.cube.gsub(/cube\_/, ‘tag\_pivot\_’)  
> model = model.singularize
> 
> # error here. How do I call activerecord given my actual model name
> 
> is in the variable called “model”?  
> num = model.count.to\_s

‘SomeClassName’.constantize should get you back the class

Fred

---

<div class="post-metadata">

**Author:** ![minkymorgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ec9cab/32.png) [@minkymorgan](https://www.ruby-forum.com/u/minkymorgan)\
**Post date:** [November 15, 2007, 5:30pm UTC](https://www.ruby-forum.com/t/dynamic-models-and-activerecord/120293/3 "2007-11-15T17:30:54Z")

</div>

Frederick

Thanks very much. this was extremely helpful !  
This was exactly what I was looking for - it works beautifully.

For readers’ reference - here is the working code, which I cleaned up  
a little.

def tag\_pivot\_column(record)  
table = record.cube.gsub(/cube\_/, ‘tag\_pivot\_’)  
model = table.singularize.camelize  
num = model.constantize.count.to\_s  
link\_to(table + " [rows=" + num + "] ", “/” \<\< table)

end

On Nov 15, 3:51 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
