Forum: Ruby on Rails Using module methods in Rake task& model?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fd0b8a5e3854d056ee30f07feedf2219?d=identicon&s=25 Mario Gr (mario517)
on 2009-04-16 00:46
Hello,

I'm trying to use some methods I'm defining in a module in a model and
my rake task.  How can I do this?  Any help would be appreciated.

I've currently got the following, but my rake task can't access the
methods in my module. Instead, I get..

undefined local variable or method `report_csv_process' for
#<Object:0x284f9e8>


/lib/module/order_process
module order_process
  def process_order(id)
     #do stuff
  end


/lib/tasks/ordering.rake

include order_process
namespace :send_report do
  task :order => :environment do
     process_order(id)
  end
end

/app/models/segment.rb
class Segment < ActiveRecord::Base
  include report_csv_process

   bla bla

end
Fd0b8a5e3854d056ee30f07feedf2219?d=identicon&s=25 Mario Gr (mario517)
on 2009-04-16 02:06
I was a little off, but still haven't quite figured it out.  I currently
have:

> /lib/module/order_process.rb
> module order_process
>   def process_order(id)
>      #do stuff
>   end
>
>
> /lib/tasks/ordering.rake
>
> include 'order_process.rb'
> namespace :send_report do
>   task :order => :environment do
>      process_order(id)
>   end
> end
>
> /app/models/segment.rb
> class Segment < ActiveRecord::Base
>   include 'report_csv_process.rb'
>
>    bla bla
>
> end


I tried removing the single quotes, without luck
2333ff4f3ccca95314050555ac2fa2c3?d=identicon&s=25 Dave (Guest)
on 2009-05-04 01:17
(Received via mailing list)
Did you ever figure out how to do this?  I'm trying to include some
module code into a custom rake task and I'm getting the same error.
Thanks.
Fd0b8a5e3854d056ee30f07feedf2219?d=identicon&s=25 Mario Gr (mario517)
on 2009-05-04 07:21
Yep.  I had to both require the file then include the module:

  require 'lib/modules/report_csv_process.rb'
  include ReportCsvProcess
2333ff4f3ccca95314050555ac2fa2c3?d=identicon&s=25 Dave (Guest)
on 2009-05-04 09:44
(Received via mailing list)
Did you ever figure out how this is done?  I'm stuck on the exact
problem (trying to use some custom module code inside a rake task).
Fd0b8a5e3854d056ee30f07feedf2219?d=identicon&s=25 Mario Gr (mario517)
on 2009-05-04 15:47
Hu?  Did you not read my last post?  You wrote almost exactly the same
questing twice.  You have to both require the file, then include the
module.


###rake_file.rb
require 'lib/modules/your_module_filename.rb'

namespace :your_rake_task do
  include your_module_name

  #do stuff
end
This topic is locked and can not be replied to.