So I have a url like the following
localhost/users/:id/posts
which gives the posts of that particular user. Now this id can be either his login (which is a string) or the id (user.id) which is technically an Integer but params[:id] is always a string. So how do I implement this an action.
@user = params[:id].is_a?(String) ? User.find_by_login(params[:id]) : User.find(params[:id])
The above code miserably fails since params[:id] is always a string. Any thoughts? Thanks.
When I’ve done this, I’ve actually had two separate controller actions–
showandshow_by_login. I feel like it’s less unpredictable that way, and I have more control.Be sure to enforce uniqueness of your logins, index them, and if
show_by_logincan’t find the record you have toraise ActiveRecord::RecordNotFoundyourself.