Call a controller from the rake file

231 views
Skip to first unread message

amvis

unread,
Jan 25, 2012, 12:39:07 AM1/25/12
to rubyonra...@googlegroups.com
How to call a controller from  the rake file, Just i have tried  to create a controller object in rake, and call the method, but i didn't get that... also how to link the helper class with the rake?

Colin Law

unread,
Jan 25, 2012, 3:05:24 AM1/25/12
to rubyonra...@googlegroups.com

I don't believe it makes sense to call controller methods from a rake
task. Put the bits you want to call in a model and you can call them.
Similarly to call view helper models does not make sense. There is
no view so what good are view helpers? Put the code in models or in
lib.

Colin

amvis

unread,
Jan 25, 2012, 3:36:34 AM1/25/12
to rubyonra...@googlegroups.com
can i use like this..

myrake file

task :my_task => [:environment] do
  rd=MofobirakesController.new
  rd.index
end

Controller file


class MofobirakesController < ApplicationController
def index
puts "hai rails3"
end
end

what is the difference  in that way to do in controller and in model..?

Colin Law

unread,
Jan 25, 2012, 3:55:17 AM1/25/12
to rubyonra...@googlegroups.com

The difference is that controller actions are intended to be run in
response to http requests. To try and run one as part of a rake
action does not make logical sense.

You ask at the top whether it would work, I have no idea whether it
would work or not and it is irrelevant because there is no sense in
doing it. If you have code that is common between a controller action
and a rake task then the code should not be in a controller action.
Move it to a model or into lib and call it from the controller and the
rake task.
What does the code that you want to call do?

Colin

>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-talk/-/bP0dF4CYiOcJ.
>
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.

--
gplus.to/clanlaw

amvis

unread,
Jan 25, 2012, 6:55:35 AM1/25/12
to rubyonra...@googlegroups.com
Basically i need to write one cron job for running the rake file from  which, i'll call one class, that have one query which will take data from some  tables and will put into another tables. This is my objective. But now created one .rb file in lib/ directory and created one .rake in task/ directory. Also call the .rb file from the rake. This is now working....

Is it enough for my operation?



Thank you
vishnu

Colin Law

unread,
Jan 25, 2012, 7:20:52 AM1/25/12
to rubyonra...@googlegroups.com

I don't understand, you say "This is now working" but then ask if it is enough.

Colin

Reply all
Reply to author
Forward
0 new messages