Search code examples
ruby-on-railsactiverecorddenormalized

Rails Model for adapting to legacy denormalized table containing multiple values


I have a legacy table that looks something like

item-detail-table
  some-key,
  item-1,
  description-1,
  item-2,
  description-2,
  item-3,
  description-3,
  item-4,
  description-4,
  item-5,
  description-5,
  other-fielda,
  other-fieldb,
  etc.

I would like to have my Rails Model return five separate objects in response to each matching record retrieved by find_by_some_key, each of which would have the logical structure:

normalized-item-detail
  some-key,
  item,
  description,
  other-fielda,
  other-fieldb,
  etc.

I just haven't done anything like this in Rails before, and would like to know the best / most idiomatic way to do something like this.

It will be a read-only model, so I can avoid all the complexities of needing to be able to update the resulting objects.

Should I have an intermediate Model representing a single sub-entry, and just have this model return an array of those that quack the way I want?

Thanks!

G.


Solution

  • You can make an TableItem-Model which has a method which gives you back the normalized classes...

    like:

    def TableItem < ActiveRecord::Base
      class NormalizedItem
         #attributes
      end
    
      def getNormalizedItems
         ret = []
         3.times do |u|
           ....
           ret << NormalizedItem.new(params)
         end
         return ret
      end
    end