So I have 3 models: Order, Item, and User.
User
has_many :items
Order
has_many :items
Item
belongs_to :user
belongs_to :order
My problem is: I want to also associate the User and Order models (like have an association: Order :belongs_to :user, User has_many :orders). I want to make a new column in the Order model that contains :user_id. But is this incorrect, since an Item already has an association with a user? Is there a fancy through relationship that can get me what I need, without adding an extra user_id field into the database?
I could technically say:
User
has_many :orders, :through => :items
Order
has_many :users, :through => :items
But this feels like a hack to me. In order to get the user associated with an order, I would have to say order.users.first (An order has_many :items, but all items will only have one user)
Also btw, I can not get rid of the Item's association with a user, as there is a period of time where an Item is created and not associated with an Order.
You're going to get yourself in trouble in the long run if you keep both kinds of associations. I think you're better off setting things up correctly now and using a rake task to conform the old data to your new schema.
So set up User -> Order -> Item
for moving forward and then create a rake task to update your database:
Now your schema is set up correctly and all your legacy data has been migrated to the new format. And best of all you're not stuck with trying to comply with the legacy format.
Sorry for the pseudocode but I'm sick today. :)