1

I am fairly new to Ruby on Rails 3 and just started to build a little application where Users can manage their Projects and Tasks.

Obviously all Projects and Tasks will have to be hidden from public and should only be visible to the User who created them.

I find this quite difficult to realise though. For example if one User creates his first Project it will be accessible via the URL /projects/1. If another User creates her first Project five seconds later it will appear under /projects/2.

How can I number Projects consecutively for each User, always starting at 1 or something more meaningful?

Also, how should I lay out my resources?

I was thinking of something like this:

resources :users do
  resources :projects
  resources :tasks
end

Is this a good way to start?

2
  • One question per question, please :) Feb 19, 2012 at 17:20
  • Welcome to StackOverflow. Remember to upvote all useful answers, including those for others' questions. And "accept" (check) the one answer that best solves your own questions.
    – Larry K
    Feb 20, 2012 at 15:02

4 Answers 4

1

You can have two ids, real hidden id and publicly visible consecutive_id. Then, in your actions, you'll have to do something similar to this:

def show
  id = params[:id]

  # instead of
  #   project = Project.find(id)
  # you now do this
  project = Project.where(user_id: current_user.id, consecutive_id: id).first

  # proceed
end

If you follow this path, you'll have to enforce sequentiality of ids yourself. Use before_create callback, for example.

1
  • Hey Sergio, thanks a lot for your help. Having two ids seems like a good idea.
    – Tintin81
    Feb 20, 2012 at 9:45
1

For a start I would get the highest value in a before_create filter and then generate a new number.

class Project < ActiveRecord::Base
  belongs_to :user

  before_create :set_per_user_id

  private
    def set_per_user_id
      val = user.projects.maximum(:document_id)
      self.document_id = val + 1
    end
end

I didn't test the code but it should be roughly working like that. Setting some validation on the document_id field to ensure uniqueness per user would be a good idea too.

(Your routes are ok so far)

1
  • Hey Thorsten, thanks for your help. I will definitely try that!
    – Tintin81
    Feb 20, 2012 at 10:12
1

thorsten's code will work for setting the project's document_id but you will still have to redefine how the resource is loaded in the edit, update, show and destroy controller actions, e.g.:

def show
  @user = User.find params[:user_id]
  @project = @user.projects.find_by_document_id params[:id]
  render
end

Honestly, unless having these id's consecutive for the user is a huge requirement, I would not suggest doing it this way. It breaks the rails convention and will make it a pain if you are trying to use libraries like cancan to authorize access to resources.

1
  • What would be an alternative then? I don't want a user to see project ids numbered like 3, 7, 28 etc. in their URLs.
    – Tintin81
    Feb 20, 2012 at 9:47
1

An important part of database-driven application design is that you should think of the database ids of your Models (Projects, Tasks, etc) as being random numbers. They mean nothin'.

And from a User Interface point of view, you should also think of the urls that your application uses as being random strings. That is, your end users should never know or care what the urls are for a given screen of information.

If you accept the above good design practices, then:

When a new Project is created, you should use filters or a "Factory" method to determine the value for the Project's "project_number" field. Eg, for a given user, they should be consecutive. But you also have to decide what should be done if a project is deleted by a user. Do you want that project's project_number to be re-used?

A different issue that you seem to hint at in the question is permissions: should user 1 be able to see user 2's projects/tasks/etc? If not, then use a gem such as cancan or similar to control permissions.

A third possible issue is the urls that will be used by your project. Standard UX principle is to ignore url formats, as I note above. But if you really want to, you can make them "pretty." There are a number of gems to help with this. See list

4
  • Hey Larry, thanks for your help. It's certainly true from an UX perspective what you're saying. But isn't it confusing for the user when he creates 3 projects and then gets urls like projects/3, projects/27and projects/34? I wonder if this can be done more user-friendly.
    – Tintin81
    Feb 20, 2012 at 10:09
  • You can relate the project url to the project itself by using one of the "pretty url" gems. List of them.. Why not use the project's name in the url rather than the project number?
    – Larry K
    Feb 20, 2012 at 14:59
  • Re: "But isn't it confusing for the user when he creates 3 projects and then gets urls like..." The only way to know is to do a UX test with real users. The answer depends on your users. Many many users won't notice the urls. Some will. Note that it is up to you (the designer) to ensure that users should never type urls. Eg offer easy searching within your app.
    – Larry K
    Feb 20, 2012 at 15:01
  • Thanks Larry, I just upvoted your answers. You really helped me a lot.
    – Tintin81
    Feb 22, 2012 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.