开发者

Missing method '%' using has_many and find() in Rails

开发者 https://www.devze.com 2023-01-15 19:06 出处:网络
I\'m getting a very odd error when I attempt to access the find method of a has_many relationship. What am I doing开发者_JS百科 wrong syntactically?

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

What am I doing开发者_JS百科 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):


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消