缺少方法“%”在 Rails 中使用 has_many 和 find()
当我尝试访问 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这一行:
应该读为:
您正在创建一个包含哈希值的数组,而不是单个哈希值。
This line:
Should read:
You were creating an array with a hash in it instead of a single hash.