0

Currently using friendly_id on my messages table. Messages are accessed like this:

site.com/messages/id e.g. site.com/messages/8

I'd like to use it like this:

site.come/messages/username e.g. site.com/messages/terrytibbs

I've got the gem working fine as I tested it with the "body" attribute in my messages table. Sent a short message "test" and tried to access it using site.com/messages/test and it works fine.

However I need to use users usernames to do this. I was wondering if there is a way to access another models attribute as my usernames are stored in my users table. I could always create a username column in my messages table and have their usernames saved there but don't like the idea of having another username column when I already have one.

Since my tables are linked I can easily find out the message sender or recipients username using their sender_id or recipient_id as that matches their ID in my users table.

Is there a quick and easy way to do what I want to do or will I have to resort to creating a username column in my messages table?

Kind regards

2 Answers 2

0

@sevenseacat is right but I believe you would have a route conflict that way:

/messages/:id
/messages/:username

Perhaps the route should be more like /user/:id/messages which would be a nested route of messages under user

resources :users do
  resources :messages, only: [:index]
end

Run rake routes to see the generated route. In your MessagesController, you will receive :user_id and easily be able to call:

User.find(params[:user_id]).messages

Make sure you have has_many :messages working in your User model.

2
  • Ahh yes! I had route conflict. I was wondering why site.com/id stopped working after the changes I made. Just wanted to keep the URL clean. There are ways I can do this but they seem to be ridiculously long ways for something so simple. I wish it didn't bother me and I could get on with things. Maybe your suggestion is my best option right now.
    – LondonGuy
    Feb 2, 2013 at 14:30
  • After reading your comment on @sevenseacat, maybe all this might be too much. If you have a current_user logged in, you can simply call .messages on him. But you'll need my suggestion if you want to display other user's messages publicly. Feb 2, 2013 at 14:58
0

This doesn't make sense to do and isn't what friendly_id is for - /messages/8 will load up a single instance of a message, while replacing it with /messages/terrytibbs will presumably load up a list of messages received/sent by the user terrytibbs.

I'd create just a normal collection route under messages, eg. get :username and then load the user -> get their messages, in that action.

1
  • That's not how I've built my system. It will load up the first message and then that first message has children which are all the other messages in that conversation. If logged in as terrytibbs and he types in another users username it will load up the conversation between him and that user. There is only ever 1 conversation between 2 users and 1 first message in that conversation.
    – LondonGuy
    Feb 2, 2013 at 14:23

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.