Forum: Ruby on Rails Database and migration question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
899f97295621824a5f482a286f5062ca?d=identicon&s=25 Jeremy Woertink (nuby2ruby)
on 2007-04-04 19:32
I have a problem with a project I am working on.

What I would like to know is, is there a way to create a field in a
database dynamically after an action from a user is done. Such that the
user can create a field on the fly and create as many as they want?


The story.

I have an ecom site and we sell products, but the price components (i.e.
tax, cost, markup ect.) are dynamic. The way the company wants it set
up, there are identifiers. The user can make as many identifiers as they
want. An identifier would be tax, cost, markup, or maybe something like
fee. The goal is that I don't care how many they make, but we need to
save these to the DB. We can make a static number of fields, but if they
make more identifiers then we have to go back manually and run a new
migration. I want to get away from this if at all possible.

If anyone has a better idea, i'm totally down with it. Any help would be
great.


Thanks,

Jeremy
E811b5337bcad19d52c8fed3a0ea0075?d=identicon&s=25 John Miller (jfmiller28)
on 2007-04-04 20:31
Jeremy Woertink wrote:
> I have a problem with a project I am working on.
>
> What I would like to know is, is there a way to create a field in a
> database dynamically after an action from a user is done. Such that the
> user can create a field on the fly and create as many as they want?
>
>
> The story.
>
> I have an ecom site and we sell products, but the price components (i.e.
> tax, cost, markup ect.) are dynamic. The way the company wants it set
> up, there are identifiers. The user can make as many identifiers as they
> want. An identifier would be tax, cost, markup, or maybe something like
> fee. The goal is that I don't care how many they make, but we need to
> save these to the DB. We can make a static number of fields, but if they
> make more identifiers then we have to go back manually and run a new
> migration. I want to get away from this if at all possible.
>
> If anyone has a better idea, i'm totally down with it. Any help would be
> great.
>
>
> Thanks,
>
> Jeremy

This is a classic one-to-many database problem.  The solution is to make
a new table (call it something like price_components) and connect it to
your Client table with a foreign key. (I think this is what you want.
I'm not sure from your description whether each client has their own set
of price components or each product does.)  Assuming your clients are in
a `clients` table it might look something like:

create_table 'price_components' do |t|
  t.column 'client_id', :integer, :null=>false
  t.column 'name', :string # tax,cost, markup, etc...
  t.column 'amount', :float
end

Then a model for it: app/model/price_component.rb

class PriceComponent < ActiveRecord::Base
  belongs_to :client
end

And in your original model: app/model/client.rb

class client < ActiveRecord::Base
  has_many :price_components

  #rest of the model goes here

end
This topic is locked and can not be replied to.