0

I have Students, each of which has a standard :id but they also have a :student_number which is unique. I would like to be able to find a view a student given only their :student_number

Here's the route I have now:

resources :students do    
    collection do
        get 'find_by_num'
    end
end 

This works, but it feels a little strange, creating a 'collection' for what I know will be a single result--that is, a 'member'. At the same time, members expect :id and I don't have one.

What would be the accepted, RESTful way of doing this?

I searched for an existing answer and found this question, but I'm interested in the route itself.

Thanks for any help!

Community
  • 1
  • 1
crowhill
  • 2,398
  • 3
  • 26
  • 55

1 Answers1

0

Ok I hope I understood correctly your question and you want something like

http://myapp.com/users/student_number to get you to the specific student.

If that is the case, you don't need to do anything with the routing, just in the Users Controller

def show
  @user = User.find_by(student_number: params[:id])
end

Then in your views, to link to the student you will have:

= link_to user.name, user_path(user.student_number)
iacobSon
  • 133
  • 10
  • That's a nice clean solution, but unfortunately, I have to keep `show` working with `:id`, which is a different column than `:student_number` The student numbers are "unique" in this particular case, but not in all cases because...I don't know. Universities have really strange database structures. – crowhill Jun 04 '15 at 22:27