Reloading the schema

3 views
Skip to first unread message

chuck

unread,
Aug 21, 2008, 1:21:49 PM8/21/08
to Dr Nic's Magic Models
I'm working on a website which allows users to upload lots of specific
information. Due to the amount of data, I wanted to give each user
their own table for this data. I started off looking at this thread:

http://railsforum.com/viewtopic.php?pid=72581

Im able to generate the table, but I get the unitialized constant
error when I try to use the new table/model without restarting
webrick.

I wanted to reload the schema if a constant isn't found, so I used
this block of code:

begin
@log_table = "user_#{current_user.id.to_s}_logs"
@log_table = @log_table.camelize.classify.constantize
rescue NameError
schema = DrNicMagicModels::Schema.new(Object)
DrNicMagicModels::Schema.class_eval "@@models = nil"
schema.load_schema

@log_table = "user_#{current_user.id.to_s}_logs"
@log_table = @log_table.camelize.classify.constantize
end

Looking at the webrick output, I can see it finding the new table:
Got a model table: user_15_logs => class User15Log


But Im still getting the error:
uninitialized constant User15Log

I'm shooting in the dark a little bit with this, but I would
appreciate any input. Also, is it possible to reload only a certain
table? I went this route because the amount of data in one table will
not scale, breaking it out into seperate tables helps. I'm worried
that reloading an entire schema, with too many tables will run into
the same scaling problem. Being able to add a specific model would be
perfect.

Thanks

chuck

unread,
Aug 21, 2008, 5:54:27 PM8/21/08
to Dr Nic's Magic Models
After a little playing around, I came across some ideas that seem to
work. I replaced the above code with this:

begin
@log_table_name = "user_#{current_user.id.to_s}_logs"
@log_table = @log_table_name.camelize.classify.constantize
rescue NameError
eval "class ::#{@log_table_name.camelize.classify} <
ActiveRecord::Base; set_table_name
'user_#{current_user.id.to_s}_logs'; end"

@log_table = @log_table_name.camelize.classify.constantize
end

This effectively loads the table by itself until the magic model is
regenerated? I think it does anyway... :)

Any comments would be greatly appreciated.
Reply all
Reply to author
Forward
0 new messages