RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Perfect timing, I has just been implementing fixtures for testing using another method, but obviously this is just great.

Thanks Ryan!

Avatar

Just so you know the plugin command that you have supplied doesn't seem to work any more. Rails says that the plugin installs but when you try to run the rake command rails can't find it. I located another plugin from Google Code that seems to work

script/plugin install http://open-id-authentication.googlecode.com/svn/trunk/open_id_authentication/

Avatar

On the subject of seed data, I was wondering what is the best approach for accessing records in code. At present I use, for example...

def self.Pending() find(1); end

...inside my seeded models like OrderStatus. Then I can test using order.status == OrderStatus.Pending. The problem is that these class methods are evaluated when the environment loads, which is necessary to load seed data in the first place. It would also be nice to be able to cache these objects for the life of the application, as they are only referring to static data after all. Is there a more appropriate convention for this which maintains the readability of the code I have? Thanks!

Avatar

Non-destructive data base conversion tool is designed for converting source database records to destination database records.

Avatar

Good job, though I agree with rjordan about the usefulness of this particular feature. Also, perhaps it's time for captcha here? I don't know why, but it seems alot of rails blogs are cluttered with spam comments.

Avatar

I love the new additions to rails, both 2.3 and 3.0; however this is NOT one of my favorites. I feel this feature was thrown in without a lot of thought about its uses. If you are doing incremental design and adding new features only as you need them, then this feature is outright broken.

For example, say I have a migration that creates an "issues" table and that table has a link to a status table on it. I use seeds.rb to insert "Open", "Closed" etc into the status table. Later I decided I need to add priority to issues and I add a priority table... wait how can I seed this table, seeds.rb has already been run and I don't want to reseed my status table, I may have user values in there now.

The only way I really see this working would be to add a seeds directory next to the migrations directory and create a seed file for each migration that gets run after the migration. Otherwise we will have to keep adding any seeds past the first into the migrations.

Avatar

What are your recommendations for sequential migrations that require seed data from previous migrations?

This generally only applies to legacy databases. I might need to move users from a legacy table to a new table and relate them to yet-to-be-seeded data (such as a default group).

Is it possible to have multiple seed files? And just reference them in the migration?

Avatar

What's the benefit of using the built in vs seed_fu?

Avatar

Thanks Ryan for another great episode. I think I can use it in my project. I am working on migrating legacy data from my postgres database to SQL server database with some added columns and schema changes. Can anyone tell me how can I accomplish this with some custom rake task? Can I use the concept in this episode to accomplish this. If yes, how do I read data from another database and insert it to my new database with some modification if needed?
My idea here is one this rake task is written, I can just run that rake task during deployment and get the data populated in the new database from the QA database.
I will appreciate your good suggestions.

Avatar

Thanks, Ryan!

It's indeed a very simple addition to rails, but it provides a good convention. I used to do this initial data loading on a custom rake task, normally called something like rake project:install, which would migrate the database and load initial data, including an admin user as well.

Avatar

O(∩_∩)O~good!

Avatar

@rwz he is using Hirb http://tagaholic.me/2009/03/13/hirb-irb-on-the-good-stuff.html

Avatar

Another great one. Thank you.

Avatar

great screencast as always.

btw, what plugin do you use to nicely display db-records in console as a table?

Avatar

Thanks Ryan.
By the way for those who use firefox you can increase the site width for this site using a style I created a while ago.
You can find it here.
http://userstyles.org/styles/15366

Avatar

If anybody gets "wrong number of arguments 1 for 0" like zzeroo's case, that should only happen when you try to create new object and that is because Rails is trying to parse nil.to_s(:db) and raises that error. As Rayn mentioned if you add nil? check on the getter method it should wotk fine!
Cheers

Avatar

Thanks for the great scrn cast Ryan!
Is there any attachments content type white list? Users should not be able to upload malicious files like .exe

Avatar

Hi,
I've been using acts_as_versioned with acts_as_paranoid, which need(ed) a custom patch to work proprely together in a project (the patch ensured that deleting a record not only sets the deleted_at column to the current date-time, but also moves it to the *_versions table and was provided on the versioned act track or somewhere like that; btw the patch on the site didn't work but I mailed the author and he gave me the correct one - obviuosly he didn't upload the patch right but was very helpful).
Since Rails 2.1.x came out with the enhanced change-tracking functionalities and acts_as_versioned stopped working, I'm stuck using Rails 2.0.2 for that project.
So, my question is: Is there a way/act/plugin to achieve what I now have with newer Rails versions?
The idea is that I need to keep track of both changes and deletions

Avatar

I've been using this for a while and it's great. Timezone support would make it fantastic!

Avatar

A great resource presented in a very compact way, so thanks. Keep up the good work.

Avatar

I have used this code in my own app but took it one step further by adding a counter cache to the tags table called taggings_count. This is where the problems start. It increments the count when taggings are added but does not decrement the count when taggings are deleted. Any suggestions would be appreciated.

Avatar

Police Ready, it's hirb (http://github.com/cldwalker/hirb/tree/master). In console require 'hirb' and do Hirb.enable. Now the records should be showed in that table format.

Avatar

Hi,

I got a A :has_many B association and I want an attribute for A, which is the max of the associated B.created_at. If I do such a find with a virtual attribute, will it be eager loaded or result in a DB call each time accessed?

Greetings

Avatar

great resource!!!

want to test your server date.

greets from germany (UTC + 1)

Avatar

Jeffery, try the trusted-params plugin at http://github.com/ryanb/trusted-params/tree/master

Avatar

Please add captcha!

Great stuff Ryan. What do you suggest to give administrators the opportunity to do mass assignment (change ownership, etc.) without making the controllers bloated?

Avatar

In the current_user method in the app_controller there is this line: @current_user = current_user_session && current_user_session.record

What does current_user_session.record do? I've checked the documentation and searched through the source but I'm no clearer. My gut instinct says that it is probably updating last_requested_at but I'd like to know for sure.

Cheers

Avatar

Thanks man these informations are very helpful

Avatar

Hey Ryan,
thank you very much for this awesome screencast! I really enjoy your stuff! Keep up the good work :)
Kind regards
Bijan

Avatar

might want to add to the command line text that to create the application.xxx and layout_helper files you need to do the following command first. otherwise the 3 files are missing.

script/generate nifty_layout

        Layout: app/views/layouts/application.html.erb
        Stylesheet: public/stylesheets/application.css
        Helper: app/helpers/layout_helper.rb

Avatar

This is exactly what I was looking for! Thanks for explaining it.

Avatar

Ryan, I have a bad feeling you have a bunch of rails developers fixing the flaws in their sites this week, especially now that you've shown all these flaws many people might not have known they had...

Thanks! Great Railscast again!

Avatar

These are interesting techniques. I have two problems with them though.

The create template's name ends with .erb although all the code in it is Javascript. That's confusing.

The JQuery.ajaxSetup method makes all forms of the application into Ajax forms. It's not flexible enough.

Avatar

Thank you for keep Rails alive, somehow I got all excited about the technology... got a couple books, watch the screencasts and develop some applications to play with it, even learn the basics of git ;-).

Lately feel like the momentum, and energy start to vanishing a bit... The brazilian podcast quit posting news, the envycast split also. Peepcode start posting some other topics to keep it profitable, which I understand and don't blame at all, but really hope you keep the rails momentum going. Its a great technology and really fun and cool way to develop applications. I'm just affraid the direction is not clear and everyday we see less and less people on the community.

Thank you Ryan for be our sensei in the Ruby on Rails development world, can't agree more thank you for all your hard work and your screencasts. Really enjoy your videos at pragmatic tv.

Best Regards Dinooz.

@Sephi-Chan Ryan mention on the previous railscast he used hirb gem something to give the output mysql like, yes I agree really cool.

Avatar

It's all the old song) But it's good to refresh knowledge.
One more thanks, Ryan!

Avatar

And people, don't forget to upgrade to 2.3.4 because of h() html escaping xss vulnerability

Avatar

Hi,

Thanks for all your screencasts !

What did you do to have this console ? It's very nice !

Sephi-Chan

Avatar

godt damn, nice episode. I'm greatfull for all the time you save me with these short to the point videos. This one was very usefull!

Avatar

Hi Ryan, love the screencasts, don't love the spam getting through your filters in the comments.

Hopefully you can improve this and share how you did it.

Thanks

Avatar

Hi Ryan,

How did you get the query executed to be displayed on the console? Another irb trick? Loved the Hirb hint!

Cheers,
Aditya

Avatar

Hey man I hit you up on twitter the other day, wondering about all the attr_accessible's in nifty-scaffold, still didn't understand when you said security issues. Now I know, cheers mate!

Avatar

Paperclip also allows for users to view other users' uploads. What's best is simply not to store files in the public directory and then use send_file. send_file would not execute any scripts.

Avatar

Hi Ryan,

Thanks a lot....for all these tutorials.....you are doing a wonderful job.....you have been a major driving force for many like me making learning rails easier and motivational..........thanks once again:)

Avatar

There is a plugin for h() issue.

http://code.google.com/p/xss-shield/

XSS Shield protects your views against cross-site scripting attacks without error-prone manual escaping with h().

Instead of:

  <h3><%= h(item.name) %></h3>
  <p><%= link_to "#{h(item.first_name)}'s stuff", :action => :view, :id => item %></p>
You will be able to write:

  <h3><%= item.name %></h3>
  <p><%= link_to "#{item.first_name}'s stuff", :action => :view, :id => item %></p>

Avatar

@Josè: I think you can use <%= raw(text) %>

Avatar

Even by the high standards one usually encounters on Railscasts, this is a fantastically concise description of major security flaws for all rails developers to be aware of.

Avatar

Ryan, about Rails 3 and the h() method: what's the function to unescape a string, i.e., the contrary of h()? :P

Thanks! Great cast today.