Ask A Question


You’re not receiving notifications from this thread.

How to use the Sentimental gem

Sascha M. asked in Gems / Libraries

Hi there,

I want to use the Sentimental gem to automatically check if an article is positive negative or neutral. This gem here: works fine in the Rails console. But I really have no clue how to use it in my scaffolded articles model - or inside the views. Do you have any ideas?


Hey Sascha! I forgot to write you back over email, but I'm planning on doing an episode on Thursday on this for you. :D

Are you interested in the simple "positive" or "negative" response or the percentage scores?


Hi Chris,

how awesome is that?! I'm very excited! Actually I'm interested in both the postive/negative and percentage scores.



Really really good episode! This is EXACTLY what I needed :P

How would you automate the app, so that you have a search field inside your rails views so that you don't have to go into the console each time?


Whoo! :)

So if you want to analyze user input, you can just setup your models like normal with your controllers. The callback will automatically analyze it.

For example, you mentioned articles. If you just have your normal scaffold or controller, all you'll have to do is add the callback to your model and the attributes and they will get automatically analyzed anytime the text is changed. Then you can just access the sentiment and score attributes like you would with any other attribute. That's it!


Ok, got it. Is there an easy way to do the following:

Let's say I want to get the sentiment of Game tweets and I have a Game model in which are many Games entered, e.g. Hitman.

Is it possible to look up the Tweets as soon someone views the page and handover the title "Hitman" to the search query automatically? Also: How can I make sure that Tweets are not getting added twice .first_or_initialize? Thanks in advance!


That could be slow, or go over the Twitter API limits, so you'd want to do that in a background job for sure. In that case, I would just load the local copy of Tweets for that game.

When you do a search and save a Tweet, you can save the keyword you searched and link it to the game, and you can also save the tweet_id so that you can use that on the first_or_initialize or however you'd like to do that.


Just FYI:

I did the redundancy check like this: do |tweet|
 find_or_create_by(body: tweet.text, tweet_id:
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 83,603+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.