开发者

Mapping non db data to rails models

开发者 https://www.devze.com 2022-12-13 11:59 出处:网络
I have an app with an Account model. Each Account belongs_to a sport, which I would usually have as a Sport model and in the DB.But as t开发者_如何转开发his isn\'t really something that will change an

I have an app with an Account model. Each Account belongs_to a sport, which I would usually have as a Sport model and in the DB. But as t开发者_如何转开发his isn't really something that will change and is not administered by the end users I thought that it might be better to put it as an integer column in the Account model and map to a hash using a class variable.

However, I need each sport to have many player_positions (which are specific to each sport). So I thought maybe I could do something like:

@@player_positions = {:rugby => [position_1, ..., ...]}

Is this good practice for static data like this or should I stick to putting it in the DB as it is relational?

I also thought maybe I could use a yaml file but not sure how I could set that up.


I would stick in the database because you are relating more than one thing to the sport (positions in addition to Account). It makes life easier if you want to use belongs_to and it will allow you to easily use SQL for counts and such (e.g., you can group accounts by sport and get a count of accounts by sport).

I have used an enumerated list of constants in some cases as opposed to tables. For example, I had a simple app with a user type of ADMIN, EDITOR and READER. Rather than put that in a roles table in the DB, I just made those constants on the User class and added a string column to the users table for role.


You don't have to use the database. http://railscasts.com/episodes/189-embedded-association might give you ideas.

0

精彩评论

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