Hi,
How can I get a model class and then search on it if I only have the
model name?
I have created an item_history to track the changes of an item in a
stock list. I use this method in the Stock model, to create the history
information:
def add_history(item, process)
history = ItemHistory.new
history.model = item.class.to_s
history.item_id = item.id
history.status = item.stock_status.name
history.comments = item.comments
history.process_name = process
if user = User.find(session[:user_id])
history.who = user.user_name
end
history.save
end
I am then able to list the history of a particular item, or all history.
In the list of all history, I use:
<% if Stock.find(:first, :conditions => ["id = ?",
history.item_id.to_i]) -%>
<td><%= link_to "#{history.model}/#{history.item_id}",
:controller => history.model.pluralize.downcase,
:action => "show",
:id => history.item_id -%></td>
<% else -%>
<td><%=h history.model -%>/<%=h history.item_id -%></td>
<% end -%>
This either outputs model/id or the same with a link to that item if the
item exists (if the action being recorded is delete, the item won’t be
there).
How do I recreate the “if Stock.find” statement to accept any model
name. I want something the will let me grab an instance of the model
class by passing history.model to it, and then let me do a find on that
class.
Is that possible?
I want to reuse this system in another model.