String的未定义方法'map' [英] undefined method 'map' for String
本文介绍了String的未定义方法'map'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
NoMethodeError:5的undefined方法'map':String:select * from users inner join user_texts on users.id = user_id where text_id =?
这里是视图:
<%form_tag user_path do
<%select_tag:text_id,Option_from_collection_for_select(text.all,:id,name,params [:text_id]),include_blank:true%>
<%= button_to'#',class:'btn btn-default'do%>
<%t(:find)%>
<%end%>
这里的控制器:
def index
如果params [:text_id] .present?
@users = User.search params [:text_id]
end
@users = User.all
end
以下是模型:
def self.search(text_id)
find_by_sql(select * from users inner join user_texts on users.id = user_id where text_id =?
,text_id)
end
当我这样做,它的工作完美:
select * from用户内部加入user_texts on users.id = user_id其中text_id = 5
解决方案
将您的方法更改为此
def self.search(text_id)
User.joins(:user_texts) .where(text_id =?,text_id)
end
更改
- 这是更多的rails-y的方式
- find_by_sql不能正常工作带查询占位符
I have a Rails 4 application. I have a search function with a select field. Now I'm getting this error:
NoMethodeError: undefined method 'map' for "5":String: select * from users inner join user_texts on users.id = user_id where text_id = ?
Here's view:
<% form_tag user_path do
<% select_tag :text_id, Option_from_collection_for_select(text.all, :id, :name, params[:text_id]), include_blank: true %>
<%= button_to '#', class: 'btn btn-default' do %>
<% t(:find) %>
<% end %>
Here's controller:
def index
if params[:text_id].present?
@users = User.search params[:text_id]
end
@users = User.all
end
Here's model:
def self.search(text_id)
find_by_sql("select * from users inner join user_texts on users.id = user_id where text_id = ?
", text_id)
end
When I do this, it works perfect:
select * from users inner join user_texts on users.id = user_id where text_id = 5
解决方案
Change your method to this
def self.search(text_id)
User.joins(:user_texts).where("text_id = ?", text_id)
end
Two reasons why you should change
- This is more rails-y way of doing it
- find_by_sql doesn't work well with query placeholders
这篇关于String的未定义方法'map'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文