开发者

Mapping Rails model attributes to fields in a legacy table

We need to use a legacy mysql db in a rails app. I already sorted out all my connections, primary keys and such. However some of the old tables have over 100 fields ( it wasn't very normilized to say the least), and most of those fields have names like par1,minvprono, etc

What is the best way to map / alias those fields to normal names?

  • methods:

    def invoice_number
      self.MINVPRONO
    end
    
    
    def invoice_number=(invoice_number)
      self.MINVPRONO = invoice_number
    end
    
  • alias

    alias_attribute :invoice_number , :MINVPRONO
    

Can't think of any other way. What is the best approach, if I want to be able to use all the "magic methods", created by method missing, i.e. find_by_invoice_number, etc?

I'm using Rail 3.0.5 on Ruby 1.9.2 and db is Mysql 5.0.45

Also one more thing, this is not a requirement but a "nice to have" thing. Eventually we will phase out legacy 开发者_C百科apps that depend on the structure of the old database and column names, and will create proper normalized tables using Rails generators and migrate data over from legacy tables. What would be the best way to prepare for it so we would have to do less of code re-factoring?


How about (updatable) views on MySQL that would mimic correct way of table design? http://dev.mysql.com/doc/refman/5.0/en/view-updatability.html


Ok Ended up doing aliases - that seems and accepted way of doing in Rails. Aliasing it is. For anyone else looking for information: rails blog . Also one thing to note for it wasn't working in rails 3 - 3.0.3, due to the lack of support in arel - that's what threw me off. Upgraded since then and it seems to be fine now.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜