Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am not able to insert data in mongodb using rails if the data type is of array type

following is the code

def friend

Twitter.configure do |config|
config.consumer_key = 'GpCZ3ppx2tvOYB7mP4FONw'
config.consumer_secret = 'ectbzaP2oLPTSMGJaR5Fj6mmdgFyVsWeM9HoZ2iwaI'
config.oauth_token ='1378905500-TMtwUpl4bVpVDAcZQ29SVONITmFLL1aCzSLHDQQ'
config.oauth_token_secret = 'SIsfYHqSKfTEUD4dutaI7zYbfJQjxXmiNXiv4vWkfY'
end
@frend= Twitter.friends("shamshul2007")
@frend.each do |hell|
puts hell.name.inspect.to_s
end

#=====
fr=Array.new
@frend.each do |f|

fr.push(f)
end


#data insertion

@fri=Friend.new
@fri.username="shamshul2007"
@fri.friend=fr  
@fri.save
end

model is friend.rb

class Friend
include Mongoid::Document
field :username, type: String
field :friend, type: Array
end

I got the following error while pushing data to mongodb

undefined method `__bson_dump__' for #<Twitter::User:0x007fbbec1a8538>
share|improve this question
add comment

2 Answers

What this code is doing ??

  a) @frend.each do |f|
  b)   @fri.friend=f 
  c) end

@fri.friend should be provided an array type value.

In above code snippet

  1) if "f" is an array??  --- line b)
  2) if "f" is an array then it is being overwritten each time the loop rotates. --- line b)

So check these things and should help you..
share|improve this answer
 
I update the code in my previous post and mention the error also.I am trying to fetch twitter friend of a specific user and saving to mongodb array type field ie friend in model friend.rb –  shamshul2007 Jun 2 '13 at 6:25
add comment

I fixed the issue and the code is as follow,I was not taking the sub array name of the field provided by twitter.

Twitter.configure do |config|
config.consumer_key = 's9J0L2Br1rutBCH3wBmnA'
config.consumer_secret = 'SNbrKPLRPOq8AAVXzUnwWv9OodO0iHMkYSXNa786qFs'
config.oauth_token ='1378905500-Xs9xbqSyzhtVCVUKYko1Zq1NXSz2ViHXCs0g4KA'
config.oauth_token_secret = 's9RAGElHJLIYNhTv0bwChBU4PcUHq2W9cQeWMSYt8'
end
@frend= Twitter.friends("shamshul2007")
fr=Array.new
@frend.each do |f|
fr.push(f.name)
end


#data insertion
puts "start saving"
@fri=Friend.new
@fri.username="shamshul2007"
@fri.friend=fr
@fri.save
puts "data saved"
end
share|improve this answer
 
mongodb array type is accepting the data now –  shamshul2007 Jun 3 '13 at 17:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.