Dittoslash Dittoslash - 11 months ago 51
Ruby Question

Datamapper resource becomes collection in session value

After running a function that (blah) returns a Datamapper object (LuckEvent), my code stores that in a Rack session. This is read in a HAML view, which consistently returns a DataMapper::Collection.

Code for getting the LuckEvent:

puts "|LX| Jumped, getting event"
session["lastLDEvent"] = ldg.get_event
puts "|LX| Jump event: #{session["lastLDEvent"].inspect}" #Consistently returns the right value.

Code for viewing it in HAML:

= session["lastLDEvent"].class if session["lastLDEvent"] #Consistently returns DataMapper::Collection.

Switching that to
consistently returns [].

Answer Source

I would try casting the result of get_event to an Array. There might be an issue with storing DataMapper::Collection in the session.

session["lastLDEvent"] = ldg.get_event.to_a

If that doesn't work, you may need to store the IDs and then re-query the events.

session["lastLDEvent"] = ldg.get_event.map(&:id)