Model Namespace?

Hello,

Sorry if this looks silly, but I trying to learn Ruby on Rails best practices.

I have a model called “Users” and a model called “Projects”. Now I need to implement a “status” field to each one of them. Each User/Project belongs_to a status.

But because they have different kind of status possibilities, I will need 2 new models: UserStatus and ProjectStatus.

Something like:

- User

– “name”

– “gender”, …

– “status_id” (this must refers to UserStatus)

- UserStatus

– “name” (like active, fired, etc)

- Project

– “name”, …

– “status_id” (this must refers to ProjectStatus)

- ProjectStatus

– “name” (opened, completed, pending, etc)

Also, to keep my application cleaner, I would like to have the ProjectStatus viewers inside my Project viewer folder.

Something like:

/app/views/projects/new.html.erb

/app/views/projects/status/new.html.erb

Is there any convention of doing this? Is that the correct way of doing what I want? What should I search for? Can “namespaces” help me? Am I picking good model names?

Thanks,

1 Like

If you're implementing states, use a state machine.

I use AASM at a preference, but there are lots of alternatives.
http://github.com/rubyist/aasm

You could add a status field to your higher level models - for
example, add 'status' to User as an integer and then store your
status:descriptions in a common status table. So you store an integer
referring to the status in your User model and if you need to display
the description, query the status table with the id.

Or you could create the status tables and setup the relation between
them - both sides - the User would have_one => :user_status and the
belongs_to in the user_status model file.

I don't know if I would put the status views below Users or Projects
though. Depending on how many controllers you have - if it's a lot
then try to put the status under the User or Project views. It's more
than likely they should be beside the other controllers as you will
probably be adding some functionality to the status controllers such
as notifying someone if a project is in X status for some amount of
time. Also I would think the status controllers would be easier to
reuse if kept higher in the hierarchy.

In any case, you can put links to change the status anywhere you like.
Simply Rails 2 contains some very good examples of this using
javascript. You might want to check it out.