Time methods in rails - ruby-on-rails-3

I see in http://api.rubyonrails.org/ rails 3.2.12 that the class Time has a set of methods like:
beginning_of_day,
beginning_of_hour,
beginning_of_month,
beginning_of_quarter,
beginning_of_week,
beginning_of_year
When I try something like t = Time.beginning_of_hour in the rails console, I get an undefined method error.
How can I experiment with these methods?

It is Date.today. There is Time.now if you want to use that.

Related

Rails undefined method 'to_i'

When the user creates a new worequest, I want to set the worequest.statuscode_id to the first entry in the statuscodes table.
The following (from worequest.rb) was working in Rails 3.1 but now I've upgraded to 3.2, it's not working.
after_initialize :defaults
def defaults
self.statuscode_id ||= Statuscode.first
end
I get
undefined method `to_i' for #<Statuscode:0x007fe934b67bd0>
Any idea why this doesn't work now? Do you know something that will work?
Thanks!

How can I find the controller and action from rails 3 route

This is basically a rails 3 version of this question. Short of parsing it myself, how can I get the components (controller, action, parameters) from a URL string?
The method ActionController::Routing::Routes#recognize_path has been deprecated, and I can't get the one it's been replaced with to work the same way:
1.9.3p125 :019 > ActionDispatch::Routing::RouteSet.recognize_path('/accounts/new', {:method => :get})
NoMethodError: undefined method `recognize_path' for ActionDispatch::Routing::RouteSet:Class
which makes sense since it's not a static method. Looking at the source didn't enlighten me either. Any pointers would be welcome.
EDIT:
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0]
Rails 3.2.3
This works for me (Ruby 1.9.2, Rails 3.1.0):
Rails.application.routes.recognize_path('/accounts/new', {:method => :get})

How do I stub ::Rails.root in rspec?

I'm writing a gem that can be used both with and without rails. In a few places I use code like
path = Rails.root if defined?(::Rails)
and I want to test this logic with rspec. I have tried stubbing it like
stub(:"::Rails").should_receive(:root).and_return("/rails")
but this does not make defined?(::Rails) evaluate to true.
Even if defined?(::Rails) is evaluated to true, you still need a Rails object to inject the method stub. There might be several ways to do this, following is a example of my preferred approach:
before(:each) do
unless defined?(::Rails)
#mocked_rails_class = true
class ::Rails
end
end
end
it do
::Rails.should_receive(:root).and_return('/rails')
your_method.should == '/rails'
end
after(:each) do
# Clean up the Rails class if it's generated by the test case.
Object.send(:remove_const, :Rails) if #mocked_rails_class
end
I'm not sure if it works on all ruby version, but at least it can work on Ruby 1.9.x.

Using Rails 3 with Geokit-rails3 location gem

I am trying to use the geokit-rails3 gem for geolocation information in a rails application. I have a simple model:
class Location < ActiveRecord::Base
def lookup_ip_information(post_ip)
ip = post_ip
location = IpGeocoder.geocode(ip)
puts location.full_address
lat = location.lat
lng = location.lng
end
end
When I call this method with request.remote_ip from my controller, it's throwing an error: uninitialized constant Location::IpGeocoder
I just had this issue and resolved it by entering the full
Geokit::Geocoders::IpGeocoder.geocode(request.remote_ip)
and that seemed to solve it for me.
Had the same issue and couldn't get IpGeocoder to work in Rails3 with above suggestions. Dug into the Rails3 source code for the gem. Looks like an easy way to work around this is use the Geokit MultiGeocoder method for Rails 3.
Example:
loc = Geokit::Geocoders::MultiGeocoder.geocode(remote.request_ip)
Don't forget to set up MultiGeocoder correctly in your initializer - geokit_config.rb
Geokit::Geocoders::provider_order = [:google,:us]

rails3 unscoped within the model gets overriden by default_scope

I have this model
User.rb
default_scope :order => 'users.created_at DESC'
and
scope :ranking, lambda { unscoped { order('users.ranking DESC') }}
and still I get a to_sql that includes ORDER BY users.created_at DESC, users.ranking DESC...
can someone explain why?
I really don't want to have to call unscoped from every controller i'll be using this model in.
Thanks!
As you're discovering, default_scope is often more trouble than it's worth. If you're wanting to stick with it, you could use reorder to ignore the previous order:
scope :ranking, reorder("ranking DESC")
Not sure why #TimPost deleted my answer but I'm using rails 3.0.5 and ruby 1.9.2 for a project and when I used reorder(which works btw) it says this in the log
DEPRECATION WARNING: reorder is deprecated. Please use except(:order).order(...) instead. (called from <class:Item>
So I don't think it is fair my answer was deleted and I got dinged for a crappy response