RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Not sure if this will even get looked at after all those spam comments, but here it is:

Is there any way to add a dynamic order to the "find." For example:

def find_products
scope = Model.scoped({:order => sort_order('attribute_1')})
scope = scope.scoped :conditions => ...
end

def sort_order(default)
"#{(params[:c] || default.to_s)} #{params[:d] == 'down' ? 'DESC' : 'ASC'}"
end

(borrowed from: http://garbageburrito.com/blog/entry/447/rails-super-cool-simple-column-sorting)

My problem is passing "params" to the model - I can't seem to do it. Any help would be appreciated. These screencasts have really helped my ror education, thanks a lot.

Avatar

What about rails applications that are written in version 1.2.6? What gem do you suggest? Because javan-whenever supports only rails >= 2.1

Avatar

0,2,4,6.. looks ugly.
*/2 is better

Avatar

What would the route in routes.rb look like for this?

I am using namespaces in my project which is forcing me to have stricter routing, and don't konw how to route for the .pdf in Rails.

I mean what would the action be if I added this to a routes entry?

I know what my controller would be, but not an action.

Anyone have any ideas?

Avatar

Very cool tool, except I can't get it to work and I can't find any support forums for it either.

When I try to run the whenever command on my production box I get the following error, any ideas?

can't activate activesupport (= 2.2.2, runtime), already activated activesupport-2.3.2

Avatar

Compared to many syntaxes, cron's is very predictable and comprehensible. It's no crazier than regular expressions and anyone who understands it has my respect.

In defense of cron, I'd say it keeps your system tasks in one central location rather distributed among separate applications.

Avatar

I saw this post http://www.hashcode.eti.br/?p=282 the comparative pdf-writer, prawn and rghost, it was created by RGhost's author, very interesting result but I prefer Prawn, it's pure and works fine for my projects :)

Avatar

This one is for jQuery

<script src="http://gist.github.com/122221.js"></script>

Avatar

@Vinay there's an open issue on github for this exact problem. I haven't had a chance to fix it, but the easy solution is to just uninstall one of your activesupport gem versions.

Avatar

@javan/ryanb,

I wrote a simple runner task in scheduler.rb. When I entered 'whenever' in Terminal to see the cron jobs, I got the following error..
rubygems.rb:147:in `activate': can't activate activesupport (= 2.1.0, runtime), already activated activesupport-2.2.2 (Gem::Exception)

I was using daemons before this to perform some tasks relating to my app models. i could specify activesupport version to use in daemons. How can i do this here?

Avatar

@Bill A new version of the Whenever gem was just published that allows you to override variables on the fly. So when deploying to qa server you might do something like:

whenever --update-crontab my_app --set environment=qa

Avatar

Ryan, you stated that the cron should only be generated on the production server, but what if you have a test or qa environment. You would want it setup there, but the generated cron entries have "RAILS_ENV=production". Is there a way to set that?

Avatar

Ryanb sounds familiar in different blogs.. I'm developing an accounting system but it takes so many time in creating models in one app..Im so confused...

Avatar

@Lourdes You can view the cron output without actually updating your crontab by running `whenever` with no options locally (or on your server).

@Francis That's where the :roles => :db comes into play in the included capistrano task. By default the cron jobs get deployed to the db server, but you could customize this however you like, and could conceivably deploy different cron jobs to servers with different roles.

@Melvin Ram The nice thing about cron is you don't have to worry about a process dying.

Avatar

How does this compare to backgroundrb? It seems cleaner & will probably use less memory... but are their any down sides to this approach? Do I loose any features?

Avatar

How do you handle the case where things should only happen once, but if you have multiple webservers, every webserver will run the same cron action.

Eg: Cart.remove_abandoned

You probably only want this to be call once only, but if deployed on N number of webserver environment it will run N times?

Thanks!

Avatar

bryan, try adding "format.pdf" in your respond_to block.

Avatar

Rails: a method to develop in 1/3 the time, then spend 10x as long debugging and tracking down obscure missing modules

Avatar

Does anyone have any suggestions on how to test this on a local machine first before putting it on a server? I'm having trouble figuring out how to do that. Thanks!

Avatar

crontab only works on unix machines, therefore whenever will only work on unix machines

Avatar

@Piotr, AFAIK windows doesn't have cron so I'm assuming this will not work there.

However, the cron job should only be generated on your production machine. So if you are using Windows in development and Unix in production, then yes, you can use this.

Avatar

So no one has really answered Piotr. Does this only work on Unix machines? davide's comment seems to suggest so.

Avatar

Doesn't work for me, and I don't know why :S

When I add .pdf in my browser it gives me an empty page. In my log I'm getting a 406 error.

All I did was install Prawn and Prawnto, I made a show.pdf.prawn and added pdf.text "Hello".

So any ideas ?

Avatar

another great background-task gem is http://rufus.rubyforge.org/rufus-scheduler/

i use it to run a rake task every 15 minutes, but without cron, so it works on my windows developer machine as well

Avatar

We use a nigthly rake task that then runs everything that needs to be done, which keeps all code in ruby and nobody has to even touch the crontab

http://pragmatig.wordpress.com/2008/12/14/no-more-crontab-madness-with-a-single-night-rake-task/

Avatar

@Joshua feel free to create an issue on github if you're having trouble. You can view your whenever generated cron jobs without actually updating your crontab by running `whenever` from the command line.

Avatar

Great plugin, I only started using this a couple of days ago so am surprised to see a screencast on it :)

The only problem is on the server I was using this syntax gave an invalid cronjob:

every :saturday, :at => "4:38am"

Avatar

Thanks for the episode.

Something that might be of interest is my cron DSL http://github.com/adzap/cronos

Its only the DSL, it doesn't actually handle the cron job management but is designed to be used in plugins/gems like whenever, craken and crontab to use a very natural syntax for defining the intervals. You can also use it in irb for cron sanity checks.

Avatar

Does it work on windows also ?

Avatar

Another brilliant episode, Ryan. Keep up the great work!

Avatar

Hi
I am very new to rails, any ideas why I am getting this error when I try and apply this helper method?

"You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.-"

Thanks

Avatar

Спасибо! За скреенкасты которые вы создаете мне нравиться главное не останавливайтесь на достигнутом! Россия рулеzzz =)

Avatar

I used the same trick to extend string with all ActionView helpers http://pragmatig.wordpress.com/2009/05/30/all-actionview-helpers-on-strings/

Avatar

To those having trouble with the title helper method error:

check out app/helper/layout_helper.rb in the example source

Avatar

I get the following when I try to add a friendship:

 ArgumentError in FriendshipsController#create

Unknown key(s): Friendship

RAILS_ROOT: /Users/Jay/Ruby/bull
Application Trace | Framework Trace | Full Trace

/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/core_ext/hash/keys.rb:47:in `assert_valid_keys'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1506:in `create_belongs_to_reflection'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:994:in `belongs_to'
(eval):3:in `belongs_to'

Avatar

Thanks so much for the tutorials!
Really appreciate it!
You have no idea how helpful these tutorials are. They are one of a kind and very very well made.
Great Job!

Avatar

As comment #34 mentioned, the RSS doesn't validate at http://www.rssboard.org/rss-validator without the atom namespace tags.

I resolved this by adding the following lines to my index.rss.builder template:

xml.rss( :version=>"2.0", :"xmlns:atom"=>"http://www.w3.org/2005/Atom" ){
  xml.channel{
    #...
    xml.atom( :link, :href => feed_url, :rel => "self", :type => "application/rss+xml" )

(Prettier at http://pastie.org/493567)

Ryan, you're amazing! Thanks for 163 fantastic tutorials.

Avatar

Nadav, the 'current_user' variable is part of the authentication plugin Ryan is using, which looked like either restful_authentication or his own nifty_authentication...

Avatar

Thanks. Great episode :-)
Two quick questions:
a) Maybe I'm missing something: Where was the 'current_user' variable set ?
b) Didn't the addition of he "remove" option the the users/show view introduced the "n+1" problem? It seems that a separate query has to be issued for each friendship in order to get the friend's name. Am I right ? Should there be an 'include' clause somewhere in order to fix it ?

Thanks so much for your casts !!

Avatar

“Your comment at the beginning about where to put controller code was especially valuable.”
— 24. Brian Armstrong

Yeap Ryan Bates is my hero

Avatar

I really enjoyed this episode, and it's falling right in line with a project I'm working on.
One of the questions I have about this set up though is this: how can we set it up so that if one member of the friendship destroys the friendship, the whole thing goes and not just half?

Avatar

Is there a preferred method for translations of large amounts of copy? For example, if there is a "terms of service" or "privacy policy" page, what is the best way to handle the large amount of copy in multiple languages? Thanks so much for your help.

Avatar

Ryan, thanks for the great coverage of using jQuery with Rails. One question, is there a standard rails way to handle errors when processing ajax requests?

Avatar

Wow Ryan...sometimes I feel like I learned more here than in 4 years of computer science classes.

Your comment at the beginning about where to put controller code was especially valuable. My users controller is littered with tons of methods like that.

I don´t know how you keep making these with little or no compensation...we need to find a business model for you so that you can continue producing these...you have a gift!
Brian

Avatar

Ryan, I cant even begin to tell how much your casts have improved my career. Thank you.

I have been having an issue with AuthLogic not destroying the session and sometimes logging in automatically on certain actions.. After researching a little on the google groups, they talk about changing the session to the active record store instead of cookies as they talk about rails cookies not being a very good solution. Im not quite sure how to make these changes in AuthLogic, and still havent come across any discussions(yet) on it. Maybe something to discuss or add to the show notes etc...?

any advice would be great

Thanks Ryan

Avatar

I am using will paginate to limit the number of results that I should show on the page, using that with the rss feed is limiting my results to the number of results mentioned in the controller.

how can i change the code that i can have two different set of limits, one for html and one for the rss feed?

Avatar

@Ryan; nice screencast as always.

@José; I converted some work I did on my own tree_select into a plugin. Check it out at; http://github.com/stijnster/tree_select/tree/master

It allows you to display acts_as_tree models as a select-tag (in a nested way). You can do something like this;

<%= f.tree_select :category_id, Category.roots, :id, :name, :include_blank => true %>

Avatar

Without dealing with roles how useful is this?

Avatar

Nice RailsCast. I just have a quick question, what if i want to use the same fields in multiple forms, how will i achieve that?

Avatar

@zzg: Nifty scaffold is a part of Ryan's Nifty Generators:

http://github.com/ryanb/nifty-generators/