Multiple associations to the same model

I have a model which contains a large dataset which is unmodifiable. I
contains a relation of ids to names for several item types lumped
together, not idea I know.

I have a model which needs two associations, both of which will link to
this model above, but with different names.

For example, lets say the dataset contains a list of furniture. Ids 1-10
might be tables and 11-20 might be chairs.

Lets say the other model defines a room. A room should be able to have
one table and one chair. Unfortunately, as they both refer to the same
model, I’m a loss on how to do it. Not even sure if it’s possible, which
will be a real pain as I can’t modify the larget dataset at all.

Grateful for some pointers, if anyone actually understands my problem :slight_smile:

On Sun, Jul 22, 2012 at 5:13 PM, Matt H. [email protected]
wrote:

Lets say the other model defines a room. A room should be able to have
one table and one chair. Unfortunately, as they both refer to the same
model, I’m a loss on how to do it.

You could have attributes called table and chair, each referring to a
furniture. Upon saving, have a validation check that table is a table
and chair is a chair. I’d recommend doing this with a furniture_type
method, rather than directly checking the range; if you somehow manage
to be able to change this ID silliness, you can then have the method
check something else instead (such as a enumerated type attribute).

-Dave


Dave A., Cleared/Remote Ruby on Rails Freelancer
(NoVa/DC/Remote); see www.DaveAronson.com, and blogs at
www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com

On Sun, Jul 22, 2012 at 2:13 PM, Matt H. [email protected]
wrote:

For example, lets say the dataset contains a list of furniture. Ids 1-10
might be tables and 11-20 might be chairs.

Lets say the other model defines a room. A room should be able to have
one table and one chair. Unfortunately, as they both refer to the same
model, I’m a loss on how to do it. Not even sure if it’s possible, which
will be a real pain as I can’t modify the larget dataset at all.

class Furniture < ActiveRecord::Base; end

class Table < Furniture
default_scope where( … check id here… )
end

class Chair < Furniture


Hassan S. ------------------------ [email protected]

twitter: @hassan

Thanks guys, I will have a fiddle and see what I can do with your
suggestions. It’s really a rediculous way to keep the data and if it was
up to me I can guarantee it would be done differently :slight_smile:

Thanks

Got it working perfectly now. I’m not going to put much checking in, if
I leave a couple of small gaps and something gets mis-associated, I can
push harder for an overhaul of the dataset :slight_smile:

Thanks again

two premature sends today; fingers sticking to keyboard here in

90+degree San Jose - sorry!

class Furniture < ActiveRecord::Base; end

class Table < Furniture
default_scope where( … check id here… )
end

class Chair < Furniture
default_scope where( …check id here… )
end

HTH,

Hassan S. ------------------------ [email protected]

twitter: @hassan