No i don’t think this will work, for example i have the following
example :
table: customer has_many :contacts
table: contact , belongs_to :customer , table fields = :name and :email
I have a customer 1 that would like to define 3 custom fields for a
contact,
for example :
- gender , - first_name , -last_name
And another customer would like to add 2 custom fields
If would use your method, i would add 5 columns to the contacts table
There can be very much customers and so there can bee very much custom
contact fields
The other customers don’t has a interest in potential or first_name
Also you will have a problem when defining validations , potential can
be a required field for a customer
but not for another
Bases on the virtual fields i now have the following solution wich works
quite nice :
virtual_attributes
virtual_attribute_types
virtual_attributes_values
A customer can define, with a nice interface custom fields for a class
I made a acts_as_free_form plugin for ActiveRecord
that includes all the virtual fields for a class :
asts_as_free_form
This does the following :
Override the method missing
When a method is called for an object contact.gender the method get’s or
sets the value in the virtual_attribute_values
Also it defines a validate for the class wich validates the virtual
fields.
It is also possible to limit the virtual field for a class by an extra
condition :
acts_as_free_form :conditions=>‘customer_id = 1’
Now only virtual-attributes for the customer with id are usable
So a customer can have custom virutal attributes
Oh another thing , all virtual fields get written to a char field, but
on reading and setting the are transformed to the right type, for exampe
:
virutal_field :created_at , type = datetime
contact.craeted_at = Time.today
contact.save , results in a string value in database “2005-10-1
23:23:22”
when reading :
contact = Contact find(1)
contact.created_at.class = Time
The type_casting is as in ActiveRecord itself
I’m thinking of adding a table virtual_attribute_validations to define
rules for validation the virtual attribute, so i can build an nice
webinterface to define validations for an attribute
If anyone is interested in the code , let me now
Eden B. wrote:
How about writing a Ruby module that dynamically expands your table when
a
new field is needed with something like:
custom1
custom1name
Initially your table would only have a single custom field. Each user
would
define what it stands for with the custom1name field. Then when a user
requests a second custom field, your module would check to see if it
exists
and if not, update the database to add another custom field and
customname
field (custom2, custom2name). Your rails app could use javascript and
httpxmlrequest to make the whole thing feel very real-time as if the
fields
were there to begin with. Eventually your database will contain enough
custom fields for most users (10?, 20?) and your field addition code
will be
used less frequently.
I have never done this before so there may be some drawbacks, but it
seems
like it might work.
Eden