开发者

alias or select AS for legacy data in rails app

I've got a rails app that is connecting to a legacy database.

when I query the db, it returns stuff like 'userID', instead of 'id' and 'userName' instead of 'name'.

I thought it would be simple enough to just write in my controllers

:select=>'user开发者_运维问答ID AS id, userName AS name'

but when I do that, the fields I'm attempting to rename don't get returned. Other fields that I'm not aliasing are getting returned.

Is there a better way of doing this? Maybe aliasing the field names in the models?


Why dont you define the changes in your model

class User < ActiveRecord::Base
  set_primary_key "userID"
  #Use alias_attribute to define your columns to ActiveRecord standards.
  alias_attribute "name", "userName"
end

Or you can use the recomended route and create a view for your database on the server.

For more information see: http://www.slideshare.net/napcs/rails-and-legacy-databases-railsconf-2009

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜