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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 15 Nov 2007, at 15:13, minkymorgan wrote:> > 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''SomeClassName''.constantize should get you back the class Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 15 Nov 2007, at 15:13, minkymorgan wrote: > > > > > > > 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 > > ''SomeClassName''.constantize should get you back the class > > Fred--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---