Need help setting up associations for database table relatio

The domain I’m trying to model is that of a web app (already fully
developed in J2EE) with multiple customers. The web app has a large
set of features, but any given customer only has a subset of those.
Each feature also has a set of configurable parameters that alter it’s
behaviour. Each customer will have a different set of parameter values
for their features.

The intention is to build a RoR in-house web app to track the J2EE web
app and bring some sanity to our documentation and testing processes,
which are currently being performed in an adhoc, chaotic manner that
leaves critical information spread out in mutiple directories on
multiple machines. I see RoR as the lasso that can reign in this
disorder and keep the company from sinking under the weight of these
inefficiencies.

The database tables seem clear enough, but I’m having difficulty
moving away from relational database thinking to the ORM associations
paradigm. Please forgive me if some of what follows is almost
completely ignorant of the Rails Way. I’m learning RoR on the fly, as
it were - this is a stealth project, and I need to get something
impressive up and working fast, before lots of money (and momentum) get
spent going in the WRONG direction.

Here are the tables I see as necessary (along with their minimal
attributes):

CUSTOMERS
id
name

FEATURES
id
name
description

CUSTOMERS_FEATURES
customers_id
features_id

PARAMETERS
id
features_id
value_range

CUSTOMERS_PARAMETERS
customers_id
parameters_id
value

My initial hack at this in Rails associations would be:
FEATURES has_and_belongs_to_many CUSTOMERS
CUSTOMERS has_and_belongs_to_many FEATURES
(this implicitly defines CUSTOMERS_FEATURES, correct?)
FEATURES has_many PARAMETERS
PARAMETERS belongs_to FEATURES
CUSTOMERS has_and_belongs_to_many PARAMETERS
PARAMETERS has_and_belongs_to_many CUSTOMERS
(implicitly defines CUSTOMERS_PARAMETERS?)

I won’t be able to try this out until this evening, but is this even in
the ballpark of the correct approach?

Nice job. Sounds like you understand the mapping pretty well.

You have one tiny problem though: customers_parameters has an
additional
field which isn’t suited for has_and_belongs_to_many. There’s ways to do
it
with has_and_belongs_to_many, but they are not favorable and have been
deprecated.

You actually want an association called has_many :through. In order to
do
that, you need to redesign your customers_paramters table… Rename it
to a
pluralized model. In this case, I’ll use ‘configurations’

configurations

id
customer_id
parameter_id
value

Here’s how your classes would look:

class Customer < ActiveRecord::Base
has_and_belongs_to_many :features
has_many :configurations
has_many :parameters, :through => :configurations
end

class Feature < ActiveRecord::Base
has_and_belongs_to_many :customers
has_many :parameters
end

class Parameter < ActiveRecord::Base
belongs_to :feature
has_many :configurations
has_many :customers, :through => :configurations
end

class Configuration < ActiveRecord::Base
belongs_to :customer
belongs_to :parameter
end

See
http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000530and
the surrounding topics for more specific information on this.

Good luck! I hope your application goes well. Feel free to ask for
additional help if there’s something here you don’t understand.