Rails - Accessing module method in model -
so, i've read all:
- best way load module/class lib folder in rails 3?
- rails doesn't load module lib
- rails /lib modules and
- how create , use module using ruby on rails 3?
but can't make work. here's situation:
i have calc_distance(place1, place2)
method , attribute places
user
model , want define method calc_total_distance
in user
model.
i want access calc_distance
method through utils
lib , not load whole utils when using it.
in /lib/utils.rb
module utils def calc_distance a, b # blah blah blah end end
in /config/application.rb
have:
config.autoload_paths += %w(#{config.root}/lib)
in console, can include utils
calc_distance(place1, place2)
, works. utils::calc_distance(place1 place2)
doesn't work ...
extra-question can ?
then in user.rb
model:
def get_total_distance # blah blah blah dist += calc_distance(place1, place2) # blah blah blah end
returns me undefined method 'include' #<user:0x00000006368278>
and
def get_total_distance # blah blah blah dist += utils::calc_distance(place1, place2) # blah blah blah end
returns me undefined method 'calc_distance' utils:module
how can achieve this, knowing prefer second method (which reckon, doesn't load whole utils module ...
class athlete < activerecord::base include utils def get_total_distance # blah blah blah dist += calc_distance(place1, place2) # blah blah blah end end
Comments
Post a Comment