I'm having trouble fetching an array of dates from my database using my rails controller, which is then utilized by Javascript for rendering a calendar. It seems to be functioning properly when I test it in the rails console, but not in the actual view. Any suggestions on what might be causing this issue? Below is the code snippet.
Gear has_many line_items
LineItem belongs_to Gear
Javascript Variable
var $myBadDates = new Array("<%= @gear.line_items.rented %>");
View that is being returned.
var $myBadDates = new Array("[]");
Line Item Model (shortened)
class LineItem < ActiveRecord::Base
belongs_to :gear
scope :available, where(:cart_id => nil)
def self.rented
LineItem.available.collect {|x| (x.rentstart..x.rentend).to_a}
end
end
Array from Rails Console
1.9.3-p194 :007 > g.line_items.rented
LineItem Load (0.7ms) SELECT `line_items`.* FROM `line_items` WHERE `line_items`.`gear_id` = 4 AND `line_items`.`cart_id` IS NULL
=> [[Tue, 12 Feb 2013, Wed, 13 Feb 2013, Thu, 14 Feb 2013, Fri, 15 Feb 2013, Sat, 16 Feb 2013, Sun, 17 Feb 2013, Mon, 18 Feb 2013, Tue, 19 Feb 2013, Wed, 20 Feb 2013, Thu, 21 Feb 2013], [Tue, 05 Feb 2013, Wed, 06 Feb 2013, Thu, 07 Feb 2013, Fri, 08 Feb 2013, Sat, 09 Feb 2013, Sun, 10 Feb 2013, Mon, 11 Feb 2013, Tue, 12 Feb 2013, Wed, 13 Feb 2013, Thu, 14 Feb 2013, Fri, 15 Feb 2013]]
UPDATED Working javascript code from accepted answer
var $myBadDates = <%= @gear.line_items.rented.flatten.to_json.html_safe %>;