radar / by_star

Lets you find ActiveRecord + Mongoid objects by year, month, fortnight, week and more!
http://ryanbigg.com
MIT License
1.05k stars 73 forks source link

Using by_star outside Rails, by_quarter error #47

Closed mongeta closed 9 years ago

mongeta commented 9 years ago

I'm using by_star in a ruby script using also Activerecord.

When I use Model.by_quarter(1), I'm getting

NoMethodError: undefined method `in?' for 1:Fixnum

The culprit is line 68 at normalization.rb, method method quarter_fixnum wich checks if the month passed is 1..4

        raise ParseError, 'Quarter number must be between 1 and 4' unless value.in?(1..4)

I'm using extensively by_quarter(1) on some Rails projects that work perfectly

thanks,

johnnyshields commented 9 years ago

The in? method is provided by ActiveSupport, which is a dependency of ActiveRecord and this library. Can you check if it is defined in active_support/core_ext/object/inclusion.rb in the version you are using?

A workaround would be to change this to:

        raise ParseError, 'Quarter number must be between 1 and 4' unless (1..4).include?(value)

which is equivalent and supported by native Ruby, however, the bigger problem here is "why isn't ActiveSupport being included properly?"

radar commented 9 years ago

It's likely that the correct part of AS isn't being loaded here. I think we should probably just explicitly specify that this part of AS is required.

@mongeta can you please provide us with a script that reproduces this problem?

mongeta commented 9 years ago

Simple script that reproduces the problem

require 'rubygems'
require 'active_record'
require 'by_star'

Time.zone = 'Madrid'

# your connection settings to database here

class Tax < ActiveRecord::Base
    has_many :tax_line
end

a=Tax.by_quarter(1,field: :factura_data)
mongeta commented 9 years ago

@johnnyshields Can you check if it is defined in active_support/core_ext/object/inclusion.rb in the version you are using?

I can't locate it know, I'll try again when I have my machine locally (I'm doing remotely now) ...

Also adding require 'active_support' didn't solve the problem

thanks to all,

johnnyshields commented 9 years ago

This works for me:

require 'active_record'
1.in?([1,2])   #=> true
0.in?([1,2])   #=> false

I am closing this issue since it's a problem with ByStar itself, something is strange about your Ruby and/or Rails setup.