Ruby on rails 3 在rails 3中只获取一个数据库列
如何才能仅获取数据库表中一列的数据,例如标题?我希望在引导typeahead的末尾有一个字符串数组。这应该可以:Ruby on rails 3 在rails 3中只获取一个数据库列,ruby-on-rails-3,twitter-bootstrap,typeahead,Ruby On Rails 3,Twitter Bootstrap,Typeahead,如何才能仅获取数据库表中一列的数据,例如标题?我希望在引导typeahead的末尾有一个字符串数组。这应该可以: def get_titles Yourcolumn.all.each do |i| @titles += i.title end return @titles end 如果您的数据库模型名为“Post”,列名为“title”,则 将为您提供所有标题的数组。有一种特殊方法: 这引发了一个异常:未定义的方法` Post.select(:title).all.map(
def get_titles
Yourcolumn.all.each do |i|
@titles += i.title
end
return @titles
end
如果您的数据库模型名为“Post”,列名为“title”,则
将为您提供所有标题的数组。有一种特殊方法:
这引发了一个异常:未定义的方法`
Post.select(:title).all.map(&:title)
Post.pluck :title