how to implement api in rails application - ruby-on-rails-3

In my rails application i want to use lite coins api calls. please let me know how to use this api in rails application.
I have implemented for bit coins i got this code:
from this i am getting the bit coins current value:
<% response = HTTParty.get("http://data.mtgox.com/api/2/BTCINR/money/ticker") %>
<% high = response["data"]["high"]["value"] %>
<% low = response["data"]["low"]["value"] %>
<% avg = response["data"]["avg"]["value"] %>
If anyone knows similar to this please help me out finding the current value of lite coins.

You need to parse JSON. Look into this: Parsing a JSON string in Ruby
Basically you get a hash of values and can use it as you like.
Your code won't work since you miss a parser.

Related

Show only onfollow softwares in library SQL Rails

I have a little problem with my first Ruby on Rails app.
I have 3 tables (Software, User and Library), a user can follow a software and see its follows in his library. But i want to see only no follows in his library.
I have no problem to see the follow but when I want to see only the softwares not followed by USER, i can not do it ...
Software.left_outer_joins(:libraries).where(libraries: {software_id: nil})
This code shows me all software that does not follow, but I need this information BY USER.
And this show follow softwares
#library_softwares = current_user.library_additions
With index.html
<% if #library_softwares.exists? %>
...
<%end>
Do you have an idea ?
Sorry for my english
Thx
EDIT :
I would like to reach this result:
#library_softwares = current_user.library_additions
#software = Software.all
#result = #software - #library_softwares
I hope I have understood what you want, saves the software information in a variable
#softwares = Software.left_outer_joins(:libraries).where(libraries: {software_id: nil})
and if you have a relationship between the user and the softawre you can get user information simply by going through the array of #softwares
<% unless #softwares.nil? %>
<% #softwares.each do |software| %>
<% software.user %>
<%end %>
<%end%>

ActiveRecord "select" results of model method

I have a Rails app that pulls in music from Soundcloud. This data contains a title, which I save as mix.sc_title but it's not always properly formatted. I have added an additional attribute on my Mix model which I call mix.override_title
For display on my site, I want to use the override title if available, and the sc_title in all other cases.
I have a Mix model method to do this for me
def display_title
override_title.blank? sc_title : override_title
end
Mixes#index grabs #mixes = Mix.where(:active => true) and mixes/index.html.erb looks like this:
<ul>
<% #mixes.each do |mix| %>
<li><%= link_to mix.display_title, mix %></li>
<% end %>
</ul>
As you can see, I'm not directly using any mix attributes, and so I take a huge hit when I go to the DB, and I don't actually benefit from it.
Is there a leaner way to get just the information I need? (mix.display_title)
I have tried Mix.select("display_title").where(:active => true) but it fails because display_title is not a real DB column
You can do Mix.select("sc_title, override_title").where(:active => true) and it will work, since those are the actual fields that the method uses. I don't really think getting the additional attributes gives you that much of a DB hit but sometimes selecting only what you need can be beneficial.
As you start chaining on more Arel commands, consider putting the select into a model method:
def select_active_titles
select("sc_title, override_title").where(:active => true)
end
Edit: Your link_to helper also secretly calls mix.id to link to the right mix, so make sure it's working and if not add id to the list of selected attributes.

Checking if a nested model attribute is true

So I have one model called Project, for which there is a nested model called Proposal (so every project has multiple proposals, and each proposal only belongs to one Project).
I have a column for Proposal called "winning" which just checks if one of the Proposals has won for the Project. I'd like to reference this on the Show page of the Project, but a little perplexed by the code.
What I really want to do is check if any of the proposals have status "winning"
This is what I'm trying for the Show view for Projects, but it isn't working:
<% if #project.proposals.winning %>
SUCCESSFUL
<% end %>
I feel like this should be pretty rudimentary but I'm having trouble figuring it out, thanks!
That's ideal candidate for:
<% if #idea.proposals.any? {|proposal| proposal.winning? } %>
Enumerable.any? returns true if for any array element the block returns true.
Use it instead:
<% if #idea.proposals.count{|a| a.winning } > 0 %>
Or even better to create a method for it in the Idea model:
def has_winning?
proposals.count{|a| a.winning } > 0
end
Okay, found this code on another post and it seems to be working, not sure if it's the best way to go about it though:
<% if #idea.proposals.map(&:winning).flatten %>

Rails STI Mystery - Why does type change from Class to String in view?

This is long so I hope you'll bear with me...
I have a model called Update with two subclasses, MrUpdate and TriggeredUpdate. Using single-table inheritance, added type field as a string to Update.
In my view I'm checking which type it is to decide what to display. I assumed since type is a string, I should do
<% if #update.type == 'MrUpdate' %>
This failed, i.e., it evaluated to false when the update was an MrUpdate. I noticed that at this point, #update.type.type is Class. OK, whatever, thought I, so I changed it to:
<% if #update.type == MrUpdate %>
and it worked, i.e., the comparison evaluated to true when the update was an MrUdpate. Then I did it again lower down in my view and it failed again (i.e., it evaluated to false when the update was an MrUpdate.)
Turns out the culprit is a couple of <%= link_to ... %> calls I use and make into buttons with jQuery. If I put this code in my view:
<br>
<%= #update.type.type %><br>
<%= #update.type %><br>
<%= link_to 'New Note', new_note_path(:update_id => #update.id), :class => "ui-button" %>
<br>
<%= #update.type.type %><br>
<%= #update.type %><br>
What I see is:
Class
MrUpdate
(the New Note button)
String
MrUpdate
It's changing from a class to a string! So what the heck am I doing wrong or missing here? Why should a link_to do that? First I'm not clear why it's not a string in the first place, but then really confused as to why it would change...?!? Any help or explanation would be helpful. I can just code it one way at the top and another way at the bottom, but that way madness lies. I need to understand why this is happening.
I figured out what the issue is here. Thanks to fl00r for pointing the way.
Yes, type is a reserved in Ruby 1.8.7 which tells you the class of the object you call it from. But it's also true that it is the name of the field used in Rails to indicate single-table inheriance and to store the name of the class of each instance of the subclass.
So I naively tried to access the value of the type field using #update.type. But what this was doing at the top of the view was calling the type method of the Object class in Ruby. For whatever reason, after the link_to calls, it was then access the value of the type field of the updates table.
While trying to figure this out I called #update.type in the Rails console and saw this message: "warning: Object#type is deprecated; use Object#class". Finally it registered what I was doing. When I changed my calls to:
<% if #update.class == MrUpdate %>
everything works as expected. I never saw a call to determine the type in any of the pages I found via Google about STI. This despite the fact that they all recommended using only one controller, wherein sometimes you must need to determine the class of the instance you have.
So, dumb mistake--pilot error. But maybe this will help someone else who gets tripped up on this.

Rails 3 - how to work with data got from database

I am loading from database the only row. The data are stored in variable (e.g.) #data.
In view, if I want to display the value got from database, I have to do following:
<% #data.each do |d| %>
<%=d.name %>
<%end%>
And I would like to ask you - exist any better way? I think it's a bit silly for the only row to use loop... I tried something like
<%= #data.name %>
OR
<%= #data.each.name %>
But in both cases I got the error message about bad syntax...
So to my question - is possible to get display data a bit more elegantly?
EDIT: my query: #data = Car.includes(:tyres).where("param1 = ?", params[:param1])
If you've loaded more than one model (row), then a loop is the natural construct for displaying each value. If you're really set on a one-liner, you could use some of Ruby's list comprehensions:
<%= #data.map(&:name).join(" ") -%>
I think that you are loading .all instead of .first.
In your controller,
#data = Data.where(:some => 'condition').first
or
#data = Data.find(params[:id])