RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

OK, so with seed-fu, rake db:seed RAILS_ENV=test would seed the test database. With the builtin method, it doesn't. How do I get seed data into the test environment?

(Yes, it needs to be there — there's a validates_existence_of that depends on the seed data being loaded…)

Avatar

@Georg
Joins allows a SQL fragment, so you could write :joins => "LEFT JOIN comments ON ...", according to the Rails documentation.

Avatar

I used delayed_job to process resizing in the background on Heroku:

http://blog.madeofcode.com/post/201282903/paperclip-s3-delayed-job-in-rails

Avatar

Hi Ryan,

Again nice screencast!!!

BTW I totally agree with @John.

Please add some captcha like things to stop spamming.

one personal question I want to ask you that have you ever think to change CSS of railscasts.com?

Though it is nice but yet it is same as when it launched.

Avatar

So if you call Country.delete_all won't the break all previous associations! Unless your index is based on country code which should be noted in the migration creation.

Avatar

great screencast...

is this whole technique w/ fields_for useless if you want a radio button then?

I tried hardcoding the id and name of the radio buttons, using a variable i which I indexed myself and still according to firebug they are missing an index, which means when i should have 2 groups of 2 radio buttons, i have 1 group of 4.

Avatar

Georg(e)? above is absolutely correct. Thank you by the way Ryan. This was exactly what I was looking for at this exact time, your screencasts are always great! Keep it up!

Avatar

Ryan, there is an important difference between :include and :joins that you didn't mentioned:

:include does a LEFT JOIN, whereas :joins does an INNER JOIN. Because an INNER JOIN requires matches in both tables, it can NOT be used if the second table has no entries for some records of the first table - because they will be filtered out.

So, IMHO your example "User.all(:joins => :comments, ..." is not a good one. The result will hide users which have no comments at all, so it doesn't result a full user list. In this example you should use :include instead of :joins.

Avatar

Is it possible to do this where (to use your example):
- there is another table that has a list of task categories.
- if there are n categories in the controller there would be n.build (the easy bit)
- rather than having a collection_select next to each item, the partial would loop through each of them and have the ability to provide a value. easy enough with a hidden value to do this, but I'm thinking the edit form could get ugly very quickly

Or ... is this the wrong approach completely?

The scenario I am thinking of is where I have products that have user-definable extended attributes, that each needs a value. But the extended attributes are consistent for all products in a category

Realise that this may be out of scope and the cast is old. But I'm sure this must be a common problem?

Any help would be eternally appreciated

Michael

Avatar

Why is the scoped method not documented?

Avatar

Barcode decoding software is easy to use program for both technical and non technical users

Avatar

Thanks for another great screencast Ryan. You know, I actually look forward to Mondays because of you.

Avatar

@kiko The very top of the code Ryan posted in the show notes should cause the DB queries to show... I think this is the line you need:

ActiveRecord::Base.logger = Logger.new(STDOUT) # to show logs

Take a look at the link to Hirb in the show notes or at daG's comment to show the DB query results as a table.

Avatar

Going back to basics, how did you get your user model into irb? I can do this easily with script/console but if I try User.all i get an uninitialized constant "User" error. Is there a way to have IRB auto load your models like script/console ??

Avatar

Could you show us how to change the search model to a non-database backed model? For example, let's say we implement this as you show it in the screencast, then we decide to make search.rb a class that does not inherit ActiveRecord::Base.

Avatar

Hey Ryan, why don't you use some sort of captcha for your comments to avoid all these spam posts...

Avatar

Thank you for the code, it's nice and useful

Avatar

Just a note that I found with rails 2.3.4. The flash_notice div is not getting updated when the next review is submitted. I changed

$("#new_review").before("<div id=\"flash_notice\"><%= escape_javascript(flash.delete(:notice)) %></div>");

to:

$("#flash_notice").html('<%=escape_javascript(flash.delete(:notice)) %>');

Avatar

Another great screen cast, hope you feel better soon!

Avatar

Ryan, Wanted to use nested routes with custom RESTful actions. For some reason it's not working.
Learned a lot from your screencasts.

Wish something like this exists for jquery.

Avatar

Great episode like always.

Haven't heard about the about the 'scoped' method before. Thanks

Avatar

To nest has_many through relationships, try passing the distinct keyword to the select query:

http://ultra.bohe.me/post/199125574/

Avatar

What is that scoped method? Cant find it in the api

Avatar

got a cold Ryan ?

thanks for your useful tips, I really like your "scoped" trick for chaining custom methods.

Avatar

Sorry for asking such a non related question , but how could u show the results in console in columns and showing queries too ? i was running the application and showing results after hitting console b4 , any help would b highly appreciated ....

Avatar

Really great screencast. Haven't really thought about the consequences of include and joins before.

Avatar

Thanks for the screencast! I've been wondering when to use either :join or :include. Good stuff. :)

Avatar

Great screencast as usual !

Though I have one question about your groups show view. How does this work ?

<%= render @group.comments %>

I never saw anything like this and the rails doc about render don't mention it.

Thanks btw !

Avatar

You can add this 2 lines

require 'hirb'
Hirb::View.enable

to your environment.rb :)

Avatar

Thanks for this one, as optimizing DB queries is always useful.

AND

Thanks for the funny (but true) "comments" in the screencast itself, you just made my day ^^

Avatar

Talking about the mass assignment, there is an interesting topic in Rails Core ML: http://bit.ly/4jfGa

Avatar

Since Rails 2.3.4, habtm join tables can't have a primary key. That broke my app. How can I make many-to-many double polymorphic associations now? I have a relationships table with:

  def self.up
    create_table :relationships do |t|
      t.column :origin_id, :integer, :null => false
      t.column :origin_type, :string, :null => false
      t.column :destination_id, :integer, :null => false
      t.column :destination_type, :string, :null => false
      t.column :position, :integer, :null => true
    end
    
    add_index :relationships, [:origin_id, :origin_type, :destination_id, :destination_type],
              :unique => true,
              :name => 'index_relationship_polymorphs'
  end

Any idea of how to accomplish this?

Avatar

I turned off Javascript and the site still works without the AJAX pagination. So I guess this is unobtrusive and SEO friendly too.

Avatar

Is it possible to scrape password protected pages, or will ScRUBYt! be required?

Thanks.

Avatar

Thanks for another great screencast Ryan. May I request a screencast on twitter in a rails app. I would like to see your approach to that.

Avatar

OK - I get it - if, and only if, you use the nested urls (articles/1/comments) then the magic works. If you try and move the comment into an article page directly then it doesn't.

Avatar

Sorry - but this does not work for me. The code on github appears to be just broken (e.g. the @comment = Comment.new(:article => @article) line referred to above.

I may be being very dumb but find_commentable has to return 'Commentable' as the parameter name is commentable_id right? Or do I just not get the route magic..

Avatar

How did your get to know so much?

Avatar

Matthew Hollingworth has a great gem that plays well with routes. It's worth looking into - it avoids before filters or passing around :subdomain in url parameters.

http://code.matthewhollingworth.net/articles/2009-06-02-adding-subdomains-to-rails-routing

Avatar

Rayn,
Nice work.. Keep it up.

Avatar

@chrisff (post 51) and @Kevin Triplett (post 114)

I need to create custom validate message. I have a association model which has 2 associations .

For example i have list of companies as company_profile.rb
has_many:group_supported_school

In group_supported_school
i have two fields school_id and company_profile_id

associations are
belongs_to:company_profile
belongs_to:school

i need to validate the school_id here which is valid or not.

Its working fine but validation saying as default
Group supported school is invalid .

can u suggest me the way to do

Avatar

What does the "False" do? Thanks.

Avatar

As for the "#1 Mass Assignment" security issue, I largely prefer Merb's approach: filtering sensitive attributes should be done in the controller.

Look the 'param_protected' plugin for Rails.

Avatar

fyi, I recently posted a screencast on my new SaasRamp plugin for doing recurring billing for software as a service (saas)
http://www.vaporbase.com/postings/SaasRamp_Screencast

Avatar

Ryan,

I really appreciate your screencasts! I am fairly new to RoR and I am wondering whether or not I need to add "accepts_nested_attributes_for ..." to the parent? I currently have this functionality working but I am curious if this is a deprecated requirement or what since I do not see you using it and it appears to work successfully without it. Much of the other reading/online resources show "accepts_nested_attributes_for ..." in the examples and I do see it on the Rails API pages. If I do need to incorporate "accepts_nested_attributes_for ..." when and why is it required?

Thanks ... Jeremiah

Avatar

Ryan,
Nice tutorial though, like always. :)
You should do something about these spam comments.

Avatar

This was a great episode! Thank you.

Avatar

Thanks a lot Ryan. You are contributions are really great.
:)

Avatar

I ended up creating a simple fork of vestal_versions so that I could specify which columns to include or exclude. Details are at http://github.com/sthapit/vestal_versions