mysql - Show current user as "Me" in select tag -
i have used below-written code select users
def user_for_select user.pluck(:name, :id).unshift(['all', 'all']) end
but want display current user's name "me" in select tag.
you can like:
def user_for_select user .pluck("case when id = #{current_user.id} 'me' else name end name, id") .unshift(['all', 'all']) end
tested in own rails console (rails 4.2.6):
admin.pluck("case when id = #{u.id} 'me' else full_name end name, id").unshift(['all', 'all']) # (0.6ms) select case when id = 7 'me' else full_name end name, id "admins" # => [["all", "all"], ["arup rakshit", 1], ["me", 7], ["pinaki das", 2], ["mina das", 3], ["proloy das", 4], ["mouli roy", 5], ["pisi das", 6]]
edit: if method class method below.
class user < ar class << self def user_for_select(user) self .pluck("case when id = #{user.id} 'me' else name end name, id") .unshift(['all', 'all']) end end end
now view call :
user_for_select(current_user)
Comments
Post a Comment