Reputation: 751
I have a Client model, and nested in that, I have a Program model. Ex:
localhost:3000/clients/2
localhost:3000/clients/2/programs/5
In my ApplicationController class, I want to be able to load in the Client model object in the before_filter. I tried doing it like this:
@user_client = Client.find(params[:client_id])
This works for nested resources (localhost:3000/clients/2/programs/5) but it doesn't work for the client realm (localhost:3000/clients/2). If I do it this way:
@user_client = Client.find(params[:id])
It's the opposite - it works in the client realm (localhost:3000/clients/2) but not the nested resources (localhost:3000/clients/2/programs/5).
Is there a method that would work for both?
Upvotes: 0
Views: 1624
Reputation: 2432
Could you do something like:
@user_client = Client.find(params[:client_id] || params[:id])
So that if one is nil then the other one will be entered as the id.
Having them in this order means that it will always grab it if there is a :client_id
.
The dangerous item though is that the id at the end of the link is called :id
so there could be some confusion there if you are trying to identify records. Just be vigilant.
Upvotes: 2