Rails: dynamic columns/attributes on models?
To me this sound like a perfect example were you want to use a schema free NoSQL database.
Or (if you want to stick with SQL) you can have a base User
model with a has_many :attributes
association. In which a attribute is just a combination of a key
('age', 'birthday', 'hair color') and a value. Complexity comes with different datatypes and queries covering multiple attributes at the same time.
Adding dynamic attributes to a Rails model
The write_attribute method updates the attribute in the underlying table. Since your new attributes are dynamic and do not match fields from your model table, no wonder it does not work.
To add attribute dynamically you need to declare it first, and then to call setter as for regular attribute. For example:
attr_name = 'value1'
# Declaring attr_accessor: attr_name
drow.instance_eval { class << self; self end }.send(:attr_accessor, attr_name)
drow.send(attr_name + '=', 'xxx') # setter
drow.send(attr_name) # getter
The property will be saved to the instance variable @value1
.
The other way to save dynamic property is to alter that variable directly, without declaring attribute accessor:
drow.instance_variable_set("@#{attr_name}".to_sym, 'xxx') # setter
drow.instance_variable_get("@#{attr_name}".to_sym) # getter
Dynamically determining Rails model attributes
There is a columns
method on ActiveRecord model classes so you can easily get a list of attribute names:
names = Model.columns.map(&:name)
You could remove a few common ones easily enough:
names = Model.columns.map(&:name) - %w[id created_at updated_at]
The accessible_attributes
class method might also be of interest, that will tell you what has been given to attr_accessible
.
Rails and mysql - adding columns to a table dynamically based upon form values
not exactly an answer, but i think that this kind of app would benefit a lot from a document-oriented database / NOSQL
system like mongoDB.
Such systems are schema-less by design.
To add my two cents, let users make dynamic changes on the schema seems a very dangerous option in an RDBMS environment to begin with. You could end up with tables with thousands of mostly empty columns, and rails would instantiate objects with thousands of methods on them. .. and what happens when you delete a column ?
rails 3 dynamically create a model with attributes that are stored in a database table
Formtastic and Simple Form gems need a model to be present to generate the forms for you. Check out this post here. Create a Tableless Model. Then create a Temporary Model using the tableless model. Then fetch the columns for Temporary Model from a table holding the column values for that model, like it is done for Foo in that link. Using an instance of the Temporary Class you can use Formtastic and generate form for it.
Add fields to ActiveRecord model dynamically in Rails 2.2.2?
Say I wanted to allow an
administrative user to add a field to
an ActiveRecord Model via an interface
in the Rails app.
I've solved this sort of problem before by having an extra model called AdminAdditions. The table includes an id, an admin user id, a model name string, a type string, and a default value string.
I override the model's find and save methods to add attributes from its admin_additions, and save them appropriately when changed. The model table has a large text field, initially empty, where I save nondefault values of the added attributes.
Essentially the views and controllers can pretend that every attribute of the model has its own column. This means form_for
and so on all work.
Related Topics
How to Sort in Ruby/Rails on Two Fields
Is Inject the Same Thing as Reduce in Ruby
I Have a Gem Installed But Require 'Gemname' Does Not Work. Why
Sidekiq Not Deallocating Memory After Workers Have Finished
How to Backreference in Ruby Regular Expression (Regex) with Gsub When I Use Grouping
Fast Way to Get Remote Image Dimensions
Install Any Version of Ruby with Rvm on Mavericks
Same Instance Variable for All Actions of a Controller
Empty Attribute with Ruby Haml
Can Bundler Show Me Which Gems in Gemfile Have Newer Versions (Eg. Dry-Run of Bundle Update)
Rails How to Get Latitude and Longitude from String Address and Put It on Google Map