Archive for July, 2008
I was getting the following error after doing sudo gem install scrubyt and trying to run a scrubyt script
can't activate RubyInline (= 3.6.3), already activated RubyInline-3.7.0] (Gem::Exception)
I found this solution for how to fix the Scrubyt RubyInline version error, and it worked great.
sudo gem uninstall RubyInline -v 3.7.0
No Comments | July 3, 2008
MarsEdit has been awesome. I have never posted so many solutions to ruby on rails coding problems before. The desktop editor makes it so much easier to do so that I actually quite enjoy it. If you are having trouble motivating yourself to post blog entries (especially if you’re a programmer wishing to document your solutions to problems so that you don’t find yourself forgetting how to do things) go get MarsEdit. It’s great.
I needed a way to get stock quote data into my rails app. I decided to go with the yahoofinance gem.
1. install it: sudo gem install yahoofinance
2. require it in your environment file - for rails 2.1 do config.gem ‘yahoofinance’
3. Use the following code to add it to your database (make sure you have a field in your database called last_trade
YahooFinance::get_standard_quotes(params[:stock][:symbol]).each do |stock, quote|
params[:stock][:last_trade] = quote.lastTrade
end
@stock = current_user.stocks.new(params[:stock])
Note: the yahoofinacne gem hasn’t been updated for quite a while. It might not be the best way to do things anymore. It might be better to use the yahoo api to access the information you need. Plus, it might be faster. Here’s an example snippet. I’ve republished this here to make sure it doesn’t go anywhere.
require 'open-uri'
require 'csv'
def get_adjusted_close stock_symbol
puts "-- #{stock_symbol} Adjusted Close - Historical --"
url = "http://ichart.finance.yahoo.com/table.csv?s=#{stock_symbol}&d=7&e=1&f=2006&g=d&a=2&b=26&c=1990&ignore=.csv"
puts "Connecting to #{url}\n"
csv = CSV.parse(open(url).read)
csv.each{|row|
puts "#{row[0]} - #{row.last}"
}
puts "---------------------------------"
end
example_stocks = "CSCO GOOG"
print "Enter a series of stock symbols separated by spaces (example: #{example_stocks}) to retrieve the historical adjusted close.\n"
stock_symbols = gets
stock_symbols ||= example_stocks
stock_symbols.split.each{|symbol|
get_adjusted_close(symbol)
}
pastie
No Comments | July 3, 2008
Sometimes I need to compare a date field in my database to todays date. Ruby doesn’t have a Date class only Time with the method Time.now. To turn Time.now into a date, I now do the following thanks to Ruby convert Time to Date
Date.parse(Time.now.strftime('%Y/%m/%d'))
Also, here’s the Ruby Time class
No Comments | July 3, 2008
I am using the restful_authentication plugin, and I wanted to add some roles. To do so, I did the following:
1. Installed role_requirement also at github role_requirement
2. script/generate roles Role User
3. rake db:migrate
4. Created some views to manage the roles role_requirement views layouts
5. Added the roles to my user create and sessions (user) edit views/actions from restful_authentication
All the code you need to add roles to restful_authentication. Make sure you add the following just at the top of your update and create actions for your users. This is what allows you to add roles to the user.
params[:user][:role_ids] ||= []
IMPORTANT: I was getting an error: Can’t mass assign these protected attributes: role_ids when first trying to use my edit and new user form. The role_ids would not save within the has_and_belongs_to_many table roles_users. I found the answer on a russian site for how to fix the Can’t mass assign these protected attributes: role_ids error.
Once you translate it you’ll see that you need to add the following to your user.rb model.
# needed this to fix Can't mass assign these protected attributes: role_ids
# from http://rubyclub.com.ua/messages/show/1324
attr_accessible :role_ids
Also, here’s how to call the role_requirement has_role? helper method from the view.
<% if current_user.has_role?("admin") %>
-- stuff --
<% end %>
Other useful links regarding roles with role_requirement and attachment_fu
- restful_authentication role_requirements helper
- Originally, I thought this was my problem, but it wasn’t
- This post had a similar problem with mass-assignl, but didn’t really offer a solution - more of a concept I didn’t quite undertand - being a rails noob and all.
2 Responses | July 2, 2008
I was looking at the option of using something like acts_as_sluggable or salty_slugs, but with the new rails this is much easier.
Just put the following in your model. to_param has replaced acts_as_sluggable
class Page < ActiveRecord::Base
def to_param
id.to_s+'-'+title.downcase.gsub(' ', '-')
end
end
And if you want to add css styling to the class of navigation you could do something like this
def slug
title.downcase.gsub(' ', '-')
end
No Comments | July 2, 2008
I needed to create random banner images for a project and have never been quite knowledgeable on the best way to do this.
I now have the following thanks to Agile Productions blog post on getting random data from the database with rails
module BannersHelper
def random_banner
# from http://www.agileproductions.com/articles/2007/11/25/a-better-way-to-select-random-rows-in-rails
count = Banner.count
banner = Banner.find(:first, :order => 'id', :offset => rand(count))
return image_tag( banner.public_filename(), :height => 180 )
end
end
No Comments | July 1, 2008
Self-Assigned ssl certificates are basically ssl certificates you create yourself on your own server. They will through a mean looking error at the user, but if you are the only user they are a great way to secure your data over the internet. Once you add an exception to the certificate, it is pretty much just as secure as any other ssl certificate as far as I currently know.
Slicehost once again has the answers:
No Comments | July 1, 2008
Make sure that within your database migration file you specify the following for any decimal column type.
t.decimal :total_assets, :default => 0, :precision => 8, :scale => 2
source: Decimal support in rails
Otherwise, the default schema is precision => 10, :scale => 0. If you are trying to do money, then you want your scale to be 2. That’s the way money works. Two decimal places. Your precision could be anything, but I followed prag dave and went with 8.
ALSO
Make sure your schema file matches this db migration file. Otherwise, you still might have problems. mysql decimal production problems
Thought: It seems a little ridiculous that the decimal’s scale for rails in mysql defaults to 0. If you are using a decimal (instead of an integer type) then it is almost certain you want at least one decimal, and it is very likely you want two decimals to deal with money. It would be nice if rails would default to a scale of 2 on a decimal column type.
No Comments | July 1, 2008