I am building an app where users can post. Each post an be upvoted and downvoted. Where the posts are displayed, I have a little form that allows the user to filter the posts. It simply passes a paramter to url called params[:post_filter] (localhost:3000/somepage?post_filter=value). Now, this works just fine and dandy, except the private method I wrote to modify the query does not work.
Here is my query:
def room
@posts = Post.where('lesson_id = ?', params[:id]).order(post_filter_params).page(params[:page]).per(30)
end
and here is my private method:
private
def post_filter_params
chosen_option = params[:post_filter].to_i == 1 or 2 or 3 ? params[:post_filter] : '1'
case chosen_option
when 1
'created_at DESC'
when 2
'upvotes DESC'
when 3
'downvotes DESC'
end
end
Now, whenever I replace the .order() value with one of the strings in my private method, everything works as planned. However, placing private method name inside the .order() value does not work. Any ideas for what is going on?
EDIT
To make sure all values are the same data type I did this but it still does not work:
def post_filter_params
param_option = params[:post_filter].to_i
chosen_option = param_option == 1 or 2 or 3 ? param_option : 1
case chosen_option
when 1
'created_at DESC'
when 2
'(upvotes - downvotes) DESC'
when 3
'downvotes DESC'
end
end
I don’t think that private method does what you think it does.
How about this?