RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

after change to use has_many :through,it doesn't auto add the "product_ids" and "product_ids=" methods.
Then how to update the association ?

Avatar

Well I mean it does but I want to surround it with tags. ie create div or a ul and then insert the yield(msg) result in it. No point in creating the ul or div if there's nothing to go in it. Do you have the site notify you when comments are posted cause that was ridiculously fast.

Avatar

@Brian, you should be able to do that without any issues (just like any normal yield call). Is it not working?

Avatar

How do you do something like yield(msg) in actionview?

Avatar

Awesome screentcast. I was looking for a way to bypass validation with my functional testing and this provided an extensible solution. Great job and thank you for all your work.

Avatar

It works now. Very odd. Yesterday everything in the Subversion Bundles menu was grayed out and the control + shift + a works as well. Great tip! Thanks!

Avatar

Yet another permalink plugin!
This one raises RecordNotFound in case given permalink doesn't exist, so you can use it like #find and catch not found errors in rescue_action_in_public

http://code.estadobeta.com/plugins/sluggable_finder/README

Avatar

@Tyler, It's hard to say. Did you set raise_delivery_errors to true? If you're using SMTP then check the logs on the SMTP server (if you can). If you're using sendmail then it's probably not configured properly. You may want to ask this on railsforum.com

Avatar

You can use the ":include" option in the find method to include multiple tables/models in one query.

If you're trying to make one model span multiple tables then I don't think that's possible without serious hacking.

Avatar

Ryan:

Thanks very much for responding to my question. I am sorry that my question sounded somewhat confusing.

Here is what I really had in mind from the question I posed. Just as you would create queries within database, by merging tables into one entity, can you do the same in Rails?

Thanks Ryan,

Alek

Avatar

Is there any reason why ActionMailer wouldn't send emails in development on localhost? I don't get any error messages and the development tail returns "Sent mail:" with the message details following.

Avatar

@Kristen, it should just work. Check your Bundles menu and see if there's a Subversion bundle. See if the key commands are set properly in that bundle, perhaps they somehow got reset?

Avatar

One Railscast a week is a bit sad, but I can live with it.
Maybe if we start donating we'll help Ryan with his great work.
I also like Michael's ideea.

Avatar

Thanks for your effort.
These casts are truely fantastic.

Avatar

When you selected the project folder in textmate and pressed control+shift+a it gave you a nice svn menu. For me, it only does a horn sound and nothing happens. Do I need to somehow enable the subversion bundle in textmate? Thank you!

Avatar

Thanks Ryan for all your hard work.
Although I could happily view a new Railscast every day, this will give me a chance to go over the older ones again.
You've created a great collection to supplement the Peepcode shows.

Avatar

Aw, only one a week :-(

I can't knock you for wanting a break but I've had no problem at all digesting them at the rate you're pumping them out :-)

Thanks for all the hard work.

Avatar

Thank you for all the great railscasts Ryan. They have truly improved my coding! Following up on some of the previous comments, I think a section of the site for suggested topics would be useful for ideas. Also maybe instead of 1 railscast for "free" each week perhaps you can do 2 per week but the 2nd one is only available to donating members and is unlocked the following week for non-donating members to view. You do excellent work and I think it's only fair you should be compensated in return for it.

Avatar

One a week is sad, but it is definitely a lot to sink my teeth into as it is... keep up the good work Ryan!

Avatar

He's gotta be running out of screencast ideas. We'll all need to email him a new idea for railscasts. All at once.

Avatar

Hey Ryan, Once a week is fine by me ;-) I'm getting information overload here!!

Avatar

Hi Ryan

these are great, you should start charging :)

Avatar

Instead of
  base.class_eval do
    extend ClassMethods
  end
you can do just
  base.extend ClassMethods
. Since you're calling another method on the same object – alias_method_chain – I suppose there's a case for using class_eval, but viewers might be interested to know there is a slightly simpler way.

Avatar

Even as I know that this is a hard work to create and produce such great screen casts every week, I will be sad if I have to wait whole week for a new pearl of wisdom.

Please reconsider you intention and make us all happy and better developers.

Avatar

Hey.

Good cast. Haven't tried this approach, but I will :).

One railscast per week.. that's sad news. Atleast it's something, and now Ryan has time to put up maybe even something more complicated cast.

Avatar

I really enjoyed this and am already refactoring 2 of my apps. Sweet!!

@ Drew

If you watch this one you will see that it's just simple organization of code.

@Stijn

I totally agree.. One railscast per week is totally unacceptable for my raging habit. I may need to join a support group or 12-step program for getting off Ryan's Rails-Crack

Avatar

Only 1 a week? My weeks will be much more boring :-)

Many thanks for all the great screencasts.

Avatar

Hey, where's the code snippet on this one?

You have us spoiled with those!

Avatar

@Alek, I'm not entirely sure what you're asking. A controller can fetch as many models as it needs in one action/view. However, if you want to apply the technique showed in this episode, you will need a name to use in the URL. In that case the focus will be on one model, not on multiple models.

Avatar

@RainChen, I don't think Rails provides an easy way to do this with the has_and_belongs_to_many association. You may want to consider using has_many :through instead.

Alternatively you can try building your own custom counter cache by using the after_add and after_remove callbacks.

Avatar

Thanks for your work Ryan

you are great

Avatar

This is not working with the has_and_belongs_to_many relation.
If there are something like:

class Post < ActiveRecord::Base
  has_and_belongs_to_many :tags, :counter_cache => true
end

will get an "ArgumentError" error:
Unknown key(s): counter_cache

how to resolve this situation?

Avatar

@seb

See Dan Manges post on Taking DRY Too Far

http://www.dcmanges.com/blog/36

Avatar

Yet another plugin.

http://svn.joshpeek.com/projects/plugins/pseudo_primary_key

Avatar

I had to do something like this for an admin page where people could see what employees had worked on on a specific day, but I needed to be able to use either an id number or username (it's an internal website for logging people's work), so I had the controller do a regex on the param. If it was all digits, then it must be an id, of it's not it must be a username. Of course I only had one controller I had to do that in, so it would probably not be a good idea in other areas.

Avatar

Hi Ryan:
Thanks for wonderful work that you are doing in the Rails community. I am just getting aquatinted with Rails and Ruby in general, and have completely gone nuts over the fact that it's wonderful to code in Rails and fun too! I think I am become a Rails addict!

I have been watching some of the most wonderful pod-cast on iTunes by you and have equally become and addict their too:-).

I have a question with regard to generating a view in which multiple controllers can be called into it. Instead of having only one model being displayed in the view, can I have multiple models, through one of the controllers, in one view? I am talking about the way one can generate a query in the database that then become another table. I am not sure if I am making sense, but very very much appreciate your response and a take on this.

Thanks so much,
Alek

Avatar

Great stuff, Ryan. It's really humbling the service you're doing to our community.

More, more, more RESTful stuff, please. This is the future.

Avatar

Well, another approach would be to override the find method with something like:

@class Product
  def find(*args)
    return find_by_permalink(*args) if !args.first =~ /^\d+$/
    super
  end
end@

Or something similar, but You'd need to validate your product permalinks to not be all numbers, or else things can get messy. Still, could solve problems when you are adding this to an already existing app.

If you are doing this for a new app, then there shouldn't be much to worry about.

Oh, and of course, nothing beats having good tests to make sure no find method goes wild ;)

Avatar

Can anyone explain this in more attractive way, so that I can easily uderstand the actual concept?

Avatar

Great tip on the "footnotes" plugin!

Avatar

I've writen a permalink plugin that extends String to allow permalizing of just about any type of string, including unicode.

http://svn.robertrevans.com/plugins/Permalize/
http://robertrevans.com/article/permalize-fun

Avatar

@Bill, I would set up an ActivationsController to do this. You can have a link in the email point to this controller and the controller can activate that account. You would need to add a unique token column to the users controller so there's some way to identify it so it can't be guessed.

Avatar

@seb, good call. A before filter would work really well here, I should have mentioned it.

@Skyblaze, yes, you can add a custom route to do this as well. I don't know of an easy way to do this when using map.resources however.

Avatar

@TaQ, I believe the "sum" method uses inject under the hood, so I doubt it would give you a speed boost.

Avatar

Rich - you can use Rick Olson's permalink_fu plugin to automatically create permalinks for you. You'll want to use PermalinkFu.escape(...) in a before_save however instead of the automatic "has_permalink" as the latter doesn't update the permalink when you edit a model.

Avatar

Does Rails automatically populate the permalink field? Most permalink implementations I've seen ask you to prepopulate the permalink column.

Avatar

Great screencast!

I am left wondering, however, how you can extend this to send the user to an activation link. IOW they cant login till they click the link you send them in the email.

Is this really involved?

Avatar

Just recently I made a plugin to do exactly this: http://www.bitbutter.com/seo_urls-plugin-making-show-pages-more-findable/14

Avatar

I didn't seen this screencast yet but anyway can't we change the "id" behaviour acting on routes.rb? It is only a route issue if the last part of the url goes in params[:id] we can change that by adding a new custom route.

Avatar

It will be great to use a before_filter fo @product = Product.find(params[:id]) to avoid duplicated lines.

Your screencast is a good example to show interest of using before_filter because you need to modify find to find_by_permalink in a bunch of actions instead of modifing one before_filter.

I don't know why rails generator do not generate this before_filter. (even in the views, it does not generate a _form partial but duplicates it in new and edit rhtml file) May be an idea for a new screencast if it's not already done :)

Thanks for this video!!