@Matt and David, I'm planning to do an episode entirely on has_many :through in the near future. In the meantime, @category.products will work and eager loading also works.
@Andreas, normally it doesn't make a difference which one you choose in Rails. However, the naming of associations is an exception. IIRC using strings here will cause problems - stick with symbols as shown in all the examples.
Another nice naming convention I've learned is GroupAssignment or CategoryAssignment. Thanks to Chris McGrath for that one.
Andreas, it doesn't really matter. The difference is that if the symbol is already used inside the app, using a symbol version will only leave you with one object in ObjectSpace while using a string will instantiate multiple objects:
Ryan, silly question, but does it matter at all if you use 'category_name' or :category_name? I see that a lot in Rails, some people use symbols, some people use strings, does it make any difference?
This came just in time. *Thanks* for not using the P-word. I always wondered if I was doing it wrong when I ran across errors. Will you go deeper... do you call it differently? Category.products still works? Eager loading still works?
Hey - I love the screencasts - especially the new ipod format!
I'm afraid that this one seems to have a corrupted mp4 files. I can't run them on either iTunes for windows or my iPod. This mp4 is coorrupted about 2:19 into it...
WOW did I learn something helpful... the '.inspect' method. What a great tool for debugging! Along with the 'render :text => foo.inspect', my life just got way easier.
Suggestion/request: a screen cast on various methods of debugging?
I'm disturbing again.
I made little helper for coderay. It works well, but i think it's a bit ugly code or what you think:
http://pastie.caboo.se/71442
Some railscasts about helper methods would be great :P.
My favorite use of this technique is to link to static content. That way my static page URLs can stay really clean: http://darwinweb.net/article/Routing_Nested_Static_Content_In_Rails
@Luigi, I haven't used the globalize plugin, so I can't comment on it. I might play around with different localization techniques some day and make a screencast about it though.
@HeyHey, I used the coderay gem to make it colorized.
Adding code snippets to your website will be a real helpful addition. (I think there is a typo in today's code snippet though - remove the "]" after "find".)
I know a lot of people have also suggested putting the code snippets in with the episode, but thanks a lot for implementing my suggestion Ryan. :) Shall make things a whole lot easier.
Loving your screencasts. Most topics I think I know already, but each episode I learn some little gems, like TextMate and Rails method shortcuts, that make me that much more productive.
@Rebort, nope, not without reloading the page. However, you can use a technology called Comet to "push" data to the client. This way all users viewing that page will automatically update when a review is added. I haven't done it myself, and I'm not sure how hard it is to do in Rails.
For more information on Comet:
http://en.wikipedia.org/wiki/Comet_(programming)
1. After the initial deploy you need to copy over the database_example.yml to the "shared" directory capistrano provides. Then you just need to configure it properly and tell capistrano to copy that file to the config folder for each deployment.
2. I use dreamhost for subversion hosting. I also use it to host the media files and the email. I think the two (slicehost and dreamhost) compliment each other really well.
Oh, I forgot, there's a nice Firebug "power user demo" by Joe Hewitt (the author) @ http://video.yahoo.com/video/play?vid=cccd4aa02a3993ab06e56af731346f78.1755924&fr=
Now, for what matters, the first problem is easy to debug via firebug, too. You just go to the console (Ctr+Shift+L, by the way, realy handy time saver), click on the request and watch the response, which should be the stack trace.
As for the last error, as rails wraps the whole thing in a try catch block, then it's a bit of a hassle, but if you really can't find the error, you can just go to the response in firebug, copy it and execute it on the console, finding the guilty statement really easy :)
I mean, really, firebug is the solution for all (ok, just most) problems out there. The one that aren't solved by Firebug are solved by "Use Rails" :P
Oh, and by the way, on a slightly unrelated, another great tool to incorporate into Firefox is Chris Pederick's (did I get that right?) Web Developer Toolbar. It mostly helps on some stuff that firebug hasn't come into attention yet, such as site accessibility. Those are my two "best extensions", by far.
(If you do too much front end stuff, you ought to yourself to stop using Safari while developing. I mean, I also rather use Safari as it's faster, but for development the extensions for firefox make it really great, particularly this too I recommended)
Hey, Ryan - a common situation I come across is uploading a file via AJAX, with a progress bar (like the way Basecamp implements it for their file uploads). This could be a good idea for a future screencast.
1. This is a problem I have with many pagination solutions out there. There's a few ways to solve it.
One way is to call the class method "search_conditions" and instead of having it do the find it will just return a conditions array so you could use it in the paginate method.
Another is to call the method "paginated_search" and have it call "paginate" instead of "find".
Lastly you could have the search method accept a block which uses with_scope to set the find conditions. This way you could call "paginate" in that block and the conditions will automatically be applied.
If you need a more detailed explanation, I encourage you to make a post on railsforum.com and I'll try to reply there.
2. There is the select_date method which kind of works. However, IIRC, it still puts the date in a second hash so it won't be passed well in a GET request. You may need to make your own date selectors or just use a text field. Again, if you ask on the railsforum.com someone may have a better solution.
I can't figure out how to use paginate and also move the search into the model.
2. I want to be able to search by dates, but I couldn't find a date_select_tag so I ended up using form_for and form.date_select which makes the URL very messy in addition to messing up paginate. I was wondering how you would implement a search like the one you showed based on dates (maybe to show when your projects are due instead of just a keyword?).
@Andreas, thanks for the tip. The reason I can't do that in this case is I need the flash to stick around if it's still a normal HTML request (non-AJAX). I don't want to set it in two places so in this case I think discarding it in the RJS is the best solution.
@Matt and David, I'm planning to do an episode entirely on has_many :through in the near future. In the meantime, @category.products will work and eager loading also works.
@Andreas, normally it doesn't make a difference which one you choose in Rails. However, the naming of associations is an exception. IIRC using strings here will cause problems - stick with symbols as shown in all the examples.
Great railscast Ryan! Can you do one on has-many :through sometime? Thanks!
Another nice naming convention I've learned is GroupAssignment or CategoryAssignment. Thanks to Chris McGrath for that one.
Andreas, it doesn't really matter. The difference is that if the symbol is already used inside the app, using a symbol version will only leave you with one object in ObjectSpace while using a string will instantiate multiple objects:
irb(main):001:0> :category.object_id
=> 158498
irb(main):002:0> :category.object_id
=> 158498
irb(main):003:0> "category".object_id
=> 265090
irb(main):004:0> "category".object_id
=> 255990
Ryan, silly question, but does it matter at all if you use 'category_name' or :category_name? I see that a lot in Rails, some people use symbols, some people use strings, does it make any difference?
This came just in time. *Thanks* for not using the P-word. I always wondered if I was doing it wrong when I ran across errors. Will you go deeper... do you call it differently? Category.products still works? Eager loading still works?
Nice! I was about to call it a day but this popped. Model naming is really tricky.
I've seen a similar routing setup in Mephisto but didn't really grok it. Your screencast really made it really clear and simple! Very cool, thanks!
Ryan please do a association in Active Record screen cast!
you are great :)
It would be great to see how to integrate CodeRay to markdown (BlueCloth) or textilize (RedCloth).
@Geoff, I recompressed it and reuploaded it. Hopefully it will work now. :)
Hey - I love the screencasts - especially the new ipod format!
I'm afraid that this one seems to have a corrupted mp4 files. I can't run them on either iTunes for windows or my iPod. This mp4 is coorrupted about 2:19 into it...
Keep up the awesome work!
WOW did I learn something helpful... the '.inspect' method. What a great tool for debugging! Along with the 'render :text => foo.inspect', my life just got way easier.
Suggestion/request: a screen cast on various methods of debugging?
Oh ya, the routest thing was nice too ;)
I'll probably do an episode in the future on adding coderay to a site.
Hey. Thanks.
I'm disturbing again.
I made little helper for coderay. It works well, but i think it's a bit ugly code or what you think:
http://pastie.caboo.se/71442
Some railscasts about helper methods would be great :P.
@Hey, yeah, the stylesheet is custom made, but you are free to use it wherever you want.
http://railscasts.com/stylesheets/coderay.css
It's not perfect and it's just made to work with ruby and rhtml. Other languages might not look so hot.
Hey Ryan.
Is this CodeRay stylesheet your made, and are we allowed to use it?
I like that theme.
My favorite use of this technique is to link to static content. That way my static page URLs can stay really clean: http://darwinweb.net/article/Routing_Nested_Static_Content_In_Rails
@Luigi, I haven't used the globalize plugin, so I can't comment on it. I might play around with different localization techniques some day and make a screencast about it though.
@HeyHey, I used the coderay gem to make it colorized.
@Brian, fixed the typo.
Thanks for the comments everyone. :)
These Railscasts are great, every one of them.
Adding code snippets to your website will be a real helpful addition. (I think there is a typo in today's code snippet though - remove the "]" after "find".)
Oops. Sorry, that was a stupid question. I guess I'm getting tired.
Again - thanks for Railscasts!
Great as always, but I have a question: how do I set the @user.updating_password = true in functional test. I'm testing the changing password page =)
Hey.
Can I ask how you made that code snippet? Colorized i mean.
I know a lot of people have also suggested putting the code snippets in with the episode, but thanks a lot for implementing my suggestion Ryan. :) Shall make things a whole lot easier.
Thanks
Matt
Thank you for your screenscasts!
I used a similar approch to localize the URLs.
Probably you don't feel this need, but what do you think about Rails localization project (es. Globalize-rails)?
Cool!
Another great cast!
Thanks for reading the feedback.
dynamic update with rjs will be a great cast if you can. Thanks again Ryan.
@carmelyne, glad you like it. :)
I actually had the feature done a few days ago, I just didn't upload it until last night. It was still a chore retyping all the code.
I should probably keep the code in a subversion repository as well and add a "tag" for each episode. Hmmm.
Busy as a bee, Ryan? Love the snippets.
Thanks Ryan for another great Railscast.
Please keep them coming.
This might have been the perfect screen cast. You really nailed it, Ryan!
link_to_funtion is nice! I hope you soon will produce some screencasts that deal with helper methods.
Thanks for all your screencasts, Ryan. I always learn something even if English is not my primary language. Speak more slowly perhaps ? (joke).
let's see
Oh,very nice and cool!~
Excellent -- thanks for the clarification and link, Ryan!
Loving your screencasts. Most topics I think I know already, but each episode I learn some little gems, like TextMate and Rails method shortcuts, that make me that much more productive.
I eagerly await the next episode.
@Rebort, nope, not without reloading the page. However, you can use a technology called Comet to "push" data to the client. This way all users viewing that page will automatically update when a review is added. I haven't done it myself, and I'm not sure how hard it is to do in Rails.
For more information on Comet:
http://en.wikipedia.org/wiki/Comet_(programming)
What happens when two different people submit reviews at the same time (or nearly the same time)? Would they be able to see each other's messages?
Thanks for the great tips Nicolás!
@sthapit,
1. After the initial deploy you need to copy over the database_example.yml to the "shared" directory capistrano provides. Then you just need to configure it properly and tell capistrano to copy that file to the config folder for each deployment.
2. I use dreamhost for subversion hosting. I also use it to host the media files and the email. I think the two (slicehost and dreamhost) compliment each other really well.
Oh, I forgot, there's a nice Firebug "power user demo" by Joe Hewitt (the author) @ http://video.yahoo.com/video/play?vid=cccd4aa02a3993ab06e56af731346f78.1755924&fr=
As usual, great screencast! :)
Now, for what matters, the first problem is easy to debug via firebug, too. You just go to the console (Ctr+Shift+L, by the way, realy handy time saver), click on the request and watch the response, which should be the stack trace.
As for the last error, as rails wraps the whole thing in a try catch block, then it's a bit of a hassle, but if you really can't find the error, you can just go to the response in firebug, copy it and execute it on the console, finding the guilty statement really easy :)
I mean, really, firebug is the solution for all (ok, just most) problems out there. The one that aren't solved by Firebug are solved by "Use Rails" :P
Oh, and by the way, on a slightly unrelated, another great tool to incorporate into Firefox is Chris Pederick's (did I get that right?) Web Developer Toolbar. It mostly helps on some stuff that firebug hasn't come into attention yet, such as site accessibility. Those are my two "best extensions", by far.
(If you do too much front end stuff, you ought to yourself to stop using Safari while developing. I mean, I also rather use Safari as it's faster, but for development the extensions for firefox make it really great, particularly this too I recommended)
I've been doing something similar except the -c option - that was nice to learn (i've been using svn add * --force instead). 2 question -
1. if you ignore database.yml and also use the deprec gem to deploy, how would you then upload database.yml for production?
2. i think i remember you host your rails app at slicehost (i do too) - where do you host your SVN repository though?
Hey, Ryan - a common situation I come across is uploading a file via AJAX, with a progress bar (like the way Basecamp implements it for their file uploads). This could be a good idea for a future screencast.
@Ryan, hadn't thought of that, very well then :).
@sthapit, good questions.
1. This is a problem I have with many pagination solutions out there. There's a few ways to solve it.
One way is to call the class method "search_conditions" and instead of having it do the find it will just return a conditions array so you could use it in the paginate method.
Course.paginate(:all, :conditions => Course.search_conditions(..))
Another is to call the method "paginated_search" and have it call "paginate" instead of "find".
Lastly you could have the search method accept a block which uses with_scope to set the find conditions. This way you could call "paginate" in that block and the conditions will automatically be applied.
If you need a more detailed explanation, I encourage you to make a post on railsforum.com and I'll try to reply there.
2. There is the select_date method which kind of works. However, IIRC, it still puts the date in a second hash so it won't be passed well in a GET request. You may need to make your own date selectors or just use a text field. Again, if you ask on the railsforum.com someone may have a better solution.
Awesome screencast, thanks again Ryan! 2 quick questions:
1. I like the idea of moving the search into the model but I'm using paginate like this:
@courses = Course.paginate(:all, :conditions => [...],
:page => params[:page], :per_page => 10)
I can't figure out how to use paginate and also move the search into the model.
2. I want to be able to search by dates, but I couldn't find a date_select_tag so I ended up using form_for and form.date_select which makes the URL very messy in addition to messing up paginate. I was wondering how you would implement a search like the one you showed based on dates (maybe to show when your projects are due instead of just a keyword?).
Thanks!!
@Andreas, thanks for the tip. The reason I can't do that in this case is I need the flash to stick around if it's still a normal HTML request (non-AJAX). I don't want to set it in two places so in this case I think discarding it in the RJS is the best solution.
Ryan,
Just a small tip, instead of setting the flash and then unsetting it you can use this:
flash.now[:notice] = "..."
This way it won't be passed to the next action :)
Thank you!