0

Is there a way in Rails to manipulate database fields and corresponding accessor methods without the „nifty generators” ?

I want users, insofar they are privileged, to be able to manipulate the database structure, that is, at least, to add or delete columns. The privileged user should have the possibility to „Add new” some columns.

Say I have an Object/Table artist and it should “dynamically” receive columns as "date of birth", "has played with", "copies sold"


Not sure if it's a dup. It takes a preliminary decision whether Rails discourages from letting the user do this to begin with or or not. (if that's the case => certainly some noSQL solution)

In pure ruby at least it is easy to dynamically add an attribute to an existing Model/Class like this

Test.class_eval do; attr_accessor "new_attribute"; end

and

Test.new.new_attribute = 2 would return => 2 as expected

In order to create or manipulate a customized input mask / model: can I not manually go the same way the generators go and manually call ActiveRecord::Migration methods like add_column as well as create getter/setter-methods for ORM ?

If yes or no, in both cases, which are they to begin with?

Thanks!

von spotz
  • 875
  • 7
  • 17
  • possible duplicate of [Rails: dynamic columns/attributes on models?](http://stackoverflow.com/questions/26029700/rails-dynamic-columns-attributes-on-models) – Jakob W Feb 04 '15 at 14:22
  • It would surprise me if it was necessary to pass on a database scheme altogether. The task I have in mind is for example to give the user of a shop-system the ability to add custom product-categories and by categories I don't mean "tags". If the merchant wants to sell a type "Books" his product-model will need other fields than the type "Bicycles" or "PC Games". Let alone subdivisions of these. It would be ridiculous to use "nifty generators" for these tasks and hard-core them and run a migration for every possible category. This should be possible to do from an admin-panel. – von spotz Feb 07 '15 at 20:33
  • Have you thought of a more generic way of designing your database schema so that you wouldn't need a new table for each product category? You could use http://api.rubyonrails.org/classes/ActiveRecord/Store.html or write your own key-value store for whatever attributes that the user needs. – Jakob W Feb 09 '15 at 09:34

1 Answers1

0

I am not aware of any elegant way to allow an Object to dynamically create new columns. This would not be a good application design and would lead massive inefficiency in your database.

You can achieve a similar type of functionality you seek using ActiveRecord associations in Rails. Here's a simple example for your Artist model using a related Attributes table.

class Artist < ActiveRecord::Base
  has_many :attributes
end

class Attribute < ActiveRecord::Base
  belongs_to :artist
end

With this association, you can allow your Artist class to create/edit/destroy Attributes. ActiveRecord will use foreign keys in the database to keep track of the relationship between the two models.

If that doesn't work for you, your next best option is to look into NoSQL databases, such as MongoDB, which allow for much more flexibility in the schema. With NoSQL, you can facilitate the insertion of data without a predefined schema.

JeffD23
  • 8,318
  • 2
  • 32
  • 41
  • Hello Jeff yes I know the association documentation. Sadly the best these methods can come up with is nested/self-referential (same class) bi-directional (friendship isn't a predication of only one person) ordered (unequal) tuples. Guess there is a workaround by having the columns in table-rows in another table instead – von spotz Feb 04 '15 at 07:44