0

我有一个Program带有字段 calle的表Symbol

我想检索所有在符号中有值的程序。意思不是空字符串,也不是零。

如何使用 ActiveRecord 构建这样的查询?

这是我的代码:

<div class="control-group">
  <%= f.label :program %>
  <div class="controls">
    <%= f.select :program_id, Program.where(:symbol.exists => true).collect {|c| [c.name + " " + c.symbol, c.id]}, {}, :class => 'chzn-select' %>
  </div>
</div>

我收到此错误消息:

:symbol:Symbol 的未定义方法“存在”

有什么建议么?

4

3 回答 3

0

Instead of using below syntax:

Program.where(:symbol.exists => true)

Use below syntax to search record where symbol is not nil:

Program.where("symbol is not null")
于 2013-02-27T13:39:50.147 回答
0

Your condition should be something like

Program.where("symbol IS NOT NULL AND symbol != ''")
于 2013-02-27T13:39:58.660 回答
0

Simply put, you can't call a method directly on the symbol in your finder method. Because of the syntax, it's currently calling the #exist method on your :symbol symbol of the Symbol class.

My memory for ActiveRecord queries is a bit bad, but you could always put a simple string condition like

@programs = Program.where("symbol <> ''")

which says symbol not null and not an empty string either.

(this goes in your controller, and then you'll be able to use the controller instance variable @programs in your view for your select input.)

于 2013-02-27T13:40:32.817 回答