Dynamic models and activerecord

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

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

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. [email protected]