Pass an ActiveRecord array through the params hash in Rails -
when pass @dispensers
through params hash get:
@dispensers = "[#<dispenser id: 38084, firstname: \"x\", lastname: \"a lane\", created_at: \"2014-06-24 20:33:30\", updated_at: \"2014-06-24 20:33:30\", user_id: nil, public: true, birth_date: nil, university_id: nil, business_id: 29, branch_id: 38057, latitude: 30.3509, longitude: -97.7505, score: nil, gmaps: nil, full_name: \"laura lane\", sex: \"f\", premium: false, slug: nil, public_reviews_on: true, bio: nil>, #<dispenser id: 38102, firstname: \"x\", lastname: \"woodall\", created_at: \"2014-06-24 20:33:31\", updated_at: \"2014-06-24 20:33:31\", user_id: nil, public: true, birth_date: nil, university_id: nil, business_id: 29, branch_id: 38075, latitude: 30.3379, longitude: -97.7593, score: nil, gmaps: nil, full_name: \"ashley woodall\", sex: \"f\", premium: false, slug: nil, public_reviews_on: true, bio: nil>
i convert workable activerecord array, when run
@dispensers.to_a
see undefined method 'to_a' #<string:0x0000000ebf30c0>
update:
thanks jorge, have worked out solution.
dispensers = params[:dispensers] array = dispensers.split(',').select{|s| s =~ /dispenser id/} ids = array.map {|x| x[/\d+/]} @dispensers = dispenser.find(ids)
you don't have object. have string. can turn string array, , content of array wont become object. if want turn in object have parse string. extract keys , pass them parameters new instance of dispenser class. question related how parse this.
this structure seems complicated single method, perhaps want learn how build own parser:
http://thingsaaronmade.com/blog/a-quick-intro-to-writing-a-parser-using-treetop.html
some steps might be:
choping array symbols, , spliting objects:
array = string[1..-1].split('>,')
then have array of strings of object attributes need turn in hash key attribute , value parsed. , last step should creating new object. (this part hardest)
other option might extracting ids:
array = string.split(',').select{|s| s =~ /dispenser id/}
and then
ids = array.map {|id| id[17,10].to_i}
disclaimer: not best way. , not reusable. question, might help.
Comments
Post a Comment