缺少方法“%”在 Rails 中使用 has_many 和 find()

发布于 2024-09-18 07:46:18 字数 577 浏览 5 评论 0原文

当我尝试访问 has_many 关系的 find 方法时,出现非常奇怪的错误。

我在语法上做错了什么?

# Instructor model
class Instructor < ActiveRecord::Base
  has_many :events
end

# Event model
class Event < ActiveRecord::Base
  belongs_to :instructor
end

# Controller snip-it
i = Instructor.first
conditions = [ :start_time => params[:start]..params[:end], :submitted => true ]
@events = i.events.find(:all, :conditions => conditions)

# Error message
# NoMethodError (undefined method `%' for {:start_time=>"1283140800".."1286769600", :submitted=>true}:Hash):

I'm getting a very odd error when I attempt to access the find method of a has_many relationship.

What am I doing wrong syntactically?

# Instructor model
class Instructor < ActiveRecord::Base
  has_many :events
end

# Event model
class Event < ActiveRecord::Base
  belongs_to :instructor
end

# Controller snip-it
i = Instructor.first
conditions = [ :start_time => params[:start]..params[:end], :submitted => true ]
@events = i.events.find(:all, :conditions => conditions)

# Error message
# NoMethodError (undefined method `%' for {:start_time=>"1283140800".."1286769600", :submitted=>true}:Hash):

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

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

发布评论

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

评论(1

极度宠爱 2024-09-25 07:46:18

这一行:

conditions = [ :start_time => params[:start]..params[:end], :submitted => true ]

应该读为:

conditions = { :start_time => params[:start]..params[:end], :submitted => true }

您正在创建一个包含哈希值的数组,而不是单个哈希值。

This line:

conditions = [ :start_time => params[:start]..params[:end], :submitted => true ]

Should read:

conditions = { :start_time => params[:start]..params[:end], :submitted => true }

You were creating an array with a hash in it instead of a single hash.

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