LuxuryMode
LuxuryMode

Reputation: 33741

How do I design my model (where I need to represent a hash or array as part of the attributes)?

I'm new to Rails and database design in general. Here's what I'm trying to do: I want to create a model, call it Command, which represents a command that can be used on a command line. So each command has the command itself and a description of what it does. As we all know, though, commands have flags/options. I want to be able to associate the many flags along with their descriptions with each command. Can I somehow include the hash/array that will store these flags/options as part of the model or do I have to create a separate model for Flags and represent each command as having many flags?

Any advice/illumination would be greatly appreciated.

EDIT -

So I've heard that I can use serialized attributes. If I do this, how can I get my hash to be represented as an inner object (with the string/values) in the json representation of my model?

Upvotes: 1

Views: 77

Answers (1)

Alok Swain
Alok Swain

Reputation: 6519

Various commands use same flags. Flags like -e, -i are common to many commands. Lets take the example of ps -e and strace -e, where the same flag is used by two different commands for two different purposes. To achieve a proper database design where each flag depends on the command it is being used. This sounds like a good example of a has_and_belongs_to_many(habtm) association, (as each command has many flags and each flag can appear for many commands).

Upvotes: 4

Related Questions