Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Rails和Koala从站点检索评论_Ruby On Rails_Facebook_Comments_Koala - Fatal编程技术网

Ruby on rails 使用Rails和Koala从站点检索评论

Ruby on rails 使用Rails和Koala从站点检索评论,ruby-on-rails,facebook,comments,koala,Ruby On Rails,Facebook,Comments,Koala,我试图从不同的新闻网站上获取评论,并对其进行处理。比如说, 我有评论,但我不知道如何使用rails和koala将其存储到mysql数据库(或任何其他数据库) 非常感谢您提供的任何帮助/提示您可以使用Ruby解析JSON data = ActiveSupport::JSON.decode(json_data) 然后简单地迭代数据并使用ActiveRecord将其存储在数据库中 data["data"].each do |comment| Comment.create!(:message =&

我试图从不同的新闻网站上获取评论,并对其进行处理。比如说,

我有评论,但我不知道如何使用rails和koala将其存储到mysql数据库(或任何其他数据库)


非常感谢您提供的任何帮助/提示

您可以使用Ruby解析JSON

data = ActiveSupport::JSON.decode(json_data)
然后简单地迭代数据并使用ActiveRecord将其存储在数据库中

data["data"].each do |comment|
  Comment.create!(:message => comment.message, ...)
end

查看下面的代码:

url_commented_on = "http://techcrunch.com/2012/01/04/mechanical-engineering-community-grabcad-grabs-4-million/"
fb_comments_url = "https://graph.facebook.com/comments/?ids="+url_commented_on
fb_comments_json = open(fb_comments_url).read
fb_comments_data = ActiveSupport::JSON.decode(fb_comments_json)

# If you want to play around, this gets you the first comment
fb_comments_data[url_commented_on]["comments"]["data"].first
# and this gets you the first comment's message
fb_comments_data[url_commented_on]["comments"]["data"].first["message"]

# You could save them in your db like this:
fb_comments = fb_comments_data[url_commented_on]["comments"]["data"]
fb_comments.each do |fb_comment|
  # Your comment Model here
  fb_commenter_name = fb_comment["from"]["name"]
  fb_comment_message = fb_comment["message"]
  fb_comment_creation_date = fb_comment["created_time"]
  # Your comment Model
  comment = CommentModel.new
  comment.author = fb_commenter_name
  comment.comment_text = fb_comment_message
  # Don't overwrite your own "created_at" attribute
  # Instead, use an additional column in the db for storing the date fb gives you
  comment.fb_created_at = fb_comment_creation_date 
  comment.save!
end

特别是当我试图从facebook上获取它时。就像我不想复制和粘贴所有的文本到我的文件。通过传递我的链接,有没有什么有效的方法?