Tag: 条款

如何在rails中使用like子句查询?

我想在搜索关键字时获得数据的json格式,所以我使用LIKE子句和这样的查询 “select * from employees where fname like ? or mname like ? or lname like ? or username like ? or id like ?”, str, str, str, str, str 但我想用rails编写代码。 我的控制器中有这个代码 def showemployees str = params[:str] render json: @employee = Employee.where(Employee.employees[:fname].matches(“%#{str}%”)) or (Employee.employees[:mname].matches(“%#{str}%”)) or (Employee.employees[:lname].matches(“%#{str}%”)) or (Employee.employees[:id].matches(“%#{str}%”)) end 这个代码在我的config / routes.rb中 get ’employees/showemployees’ root […]

如何在Ruby中正确使用guard子句

在此示例中使用保护条款的正确方法是什么? def require_admin unless current_user && current_user.role == ‘admin’ flash[:error] = “You are not an admin” redirect_to root_path end end 尝试使用这些https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals惯例重写时,我不知道在哪里放flash消息