ruby on rails - How to define dynamic AR table name -
in gem have rails generator build migration. migration simple:
class migrategenerator < rails::generators::base source_root file.expand_path(file.join(file.dirname(__file__), '../generators/templates')) def create_migration template "migration_template.rb", "db/migrate/create_orders_table.rb" end end class createorderstable < activerecord::migration def self.up create_table(:my_default_name) |t| #some columns end #indexes end end class order < activerecord::base self.primary_key = :id self.table_name = 'my_default_name' end
my question: there possibility allow user define own table name? know can pass parameter generator how change defined self.table_name = 'my_default_name'
either need store mapping between user_id , table name or follow standard pattern based on user_id (eg. "_orders")
self.table_name
can both string method.
if using standard pattern:
self.table_name = "#{current_user.id}_orders"
ensure logged in before accessing model.
if random, store mapping between user id , table name of orders of user.
def self.table_name "special_" + super end
Comments
Post a Comment