为什么这个named_scope会创建一个NoMethodError?

发布于 2024-10-21 17:11:36 字数 2151 浏览 1 评论 0原文

我不知道这段代码在哪里创建了这个 NoMethodFound 错误,并且非常希望有任何有用的建议。

这是错误消息:

NoMethodError in UploadsController#create

我的模型的相关部分如下所示:

  named_scope :by_name, lambda { |marker_name| 
    {:conditions => ["marker_name = ?", marker_name]}}

  def self.parse_file(file)
    FasterCSV.foreach(file.path,:headers=>"first_row", :col_sep=>"\t") do |row|

    if $header_row == 1
       $markers = {}         # define global hash for marker id lookup
        $markers_arry = []    # define global array for marker names
        get_markers(row)
        $header_row = 0
        # done with header row; loop back to beginning for first row of actual data
        next
      end
...
 def self.get_markers(row)
    offset = 8    # this was determine by trial-&-error
    i = 0
    for col in row
      i += 1
      if i < offset
        next
      end      
      this_marker = Marker.by_name(row[col])
      $markers[row[col]] = this_marker.id   # associate the marker_name with its id
      $markers_arry[col] = row[col]
    end
  end

跟踪如下所示:

> undefined method `by_name' for
> #<Class:0xb6726e40>
> vendor/rails/activerecord/lib/active_record/base.rb:1667:in
> `method_missing'
> app/models/upload.rb:99:in
> `get_markers'
> /usr/lib/ruby/gems/1.8/gems/fastercsv-1.5.4/lib/faster_csv.rb:362:in
> `each'
> /usr/lib/ruby/gems/1.8/gems/fastercsv-1.5.4/lib/faster_csv.rb:362:in
> `each' app/models/upload.rb:91:in
> `get_markers'
> app/models/upload.rb:37:in
> `parse_file'
> /usr/lib/ruby/gems/1.8/gems/fastercsv-1.5.4/lib/faster_csv.rb:1545:in
> `each'
> /usr/lib/ruby/gems/1.8/gems/fastercsv-1.5.4/lib/faster_csv.rb:1048:in
> `foreach'
> /usr/lib/ruby/gems/1.8/gems/fastercsv-1.5.4/lib/faster_csv.rb:1222:in
> `open'
> /usr/lib/ruby/gems/1.8/gems/fastercsv-1.5.4/lib/faster_csv.rb:1047:in
> `foreach' app/models/upload.rb:25:in
> `parse_file'
> app/controllers/uploads_controller.rb:55:in
> `create'
> vendor/rails/actionpack/lib/action_controller/base.rb:1162:in
> `send'

I don't see where this code is creating this NoMethodFound error, and would really like any helpful suggestions.

Here's the error message:

NoMethodError in UploadsController#create

The relevant parts of my model looks like this:

  named_scope :by_name, lambda { |marker_name| 
    {:conditions => ["marker_name = ?", marker_name]}}

  def self.parse_file(file)
    FasterCSV.foreach(file.path,:headers=>"first_row", :col_sep=>"\t") do |row|

    if $header_row == 1
       $markers = {}         # define global hash for marker id lookup
        $markers_arry = []    # define global array for marker names
        get_markers(row)
        $header_row = 0
        # done with header row; loop back to beginning for first row of actual data
        next
      end
...
 def self.get_markers(row)
    offset = 8    # this was determine by trial-&-error
    i = 0
    for col in row
      i += 1
      if i < offset
        next
      end      
      this_marker = Marker.by_name(row[col])
      $markers[row[col]] = this_marker.id   # associate the marker_name with its id
      $markers_arry[col] = row[col]
    end
  end

The trace looks like:

> undefined method `by_name' for
> #<Class:0xb6726e40>
> vendor/rails/activerecord/lib/active_record/base.rb:1667:in
> `method_missing'
> app/models/upload.rb:99:in
> `get_markers'
> /usr/lib/ruby/gems/1.8/gems/fastercsv-1.5.4/lib/faster_csv.rb:362:in
> `each'
> /usr/lib/ruby/gems/1.8/gems/fastercsv-1.5.4/lib/faster_csv.rb:362:in
> `each' app/models/upload.rb:91:in
> `get_markers'
> app/models/upload.rb:37:in
> `parse_file'
> /usr/lib/ruby/gems/1.8/gems/fastercsv-1.5.4/lib/faster_csv.rb:1545:in
> `each'
> /usr/lib/ruby/gems/1.8/gems/fastercsv-1.5.4/lib/faster_csv.rb:1048:in
> `foreach'
> /usr/lib/ruby/gems/1.8/gems/fastercsv-1.5.4/lib/faster_csv.rb:1222:in
> `open'
> /usr/lib/ruby/gems/1.8/gems/fastercsv-1.5.4/lib/faster_csv.rb:1047:in
> `foreach' app/models/upload.rb:25:in
> `parse_file'
> app/controllers/uploads_controller.rb:55:in
> `create'
> vendor/rails/actionpack/lib/action_controller/base.rb:1162:in
> `send'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风疏影 2024-10-28 17:11:36

请注意,在 Rails 3 中,named_scope 已重命名为 scope

Note that in Rails 3, named_scope has been renamed to scope.

邮友 2024-10-28 17:11:36

您是否尝试过只执行“by_name”而不是“Marker.by_name”?

此外,Rails 动态生成许多类似的方法。例如,

Marker.find_by_name("your_name_here")

...无论如何都应该有效。

Have you tried doing just "by_name" instead of "Marker.by_name" ?

Also, Rails dynamically generates many similar methods. For instance,

Marker.find_by_name("your_name_here")

... Which ought to work regardless.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文