RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

hash = { :key => "value"} can be shorted to hash = { key: "value"} in Ruby 1.9 and up. You are likely running an older version of Ruby.

Avatar

Actually, koriroys' answer is the better way to go.

Avatar

It turns out I have xcode 4.3.2 installed but not the command line tools (xcode-> preferences -> downloads). When I ran brew --config it showed my Clang as: 2.1 build 163. After downloading/installing the xcode command line tools brew --config shows Clang: 3.1 build 318.

Then I reinstalled postgresql (brew uninstall postgresql / brew install postgresql) and restarted my server and BAM my postgres command line tools worked...no more segmentation fault.

Answer found on the homebrew github page: https://github.com/mxcl/homebrew/issues/10979

Avatar

+1 for TorqueBox. An episode deploying TorqueBox to Engine Yard would be fantastic.

Avatar

better still, how about making share_review an instance method? e.g., current_user.share_review(movie) (also it should take a movie object, not a movie URL..)

Avatar

I get the following error when I try this on Ubuntu 10.04. Does anyone have any suggestions?

OAuth::Unauthorized (401 Unauthorized):
oauth (0.4.6) lib/oauth/consumer.rb:216:in token_request'
oauth (0.4.6) lib/oauth/consumer.rb:136:in
get_request_token'
.....

Avatar

Sure. See their documentation about "Configuration and Variables": https://devcenter.heroku.com/articles/config-vars

sh
heroku config:add TWITTER_CONSUMER_KEY=...
Avatar

That is where the constants are stated, but they are not defined there. I think Dominik is on the right track.

Avatar

Thanks. Any idea how I would define it on Heroku?

Avatar

Wow thank you @Barwin! I've been wrestling with this for the past 2 days and just couldn't figure out what I was doing wrong. I also had a respond_to block and just needed to pop in a format.js. Works like a champ.

Avatar

Perhaps it's a dumb question but imagine that you have 400.000 products

in this chain of wheres conditions (with pagination)

ruby
 products = Product.order(:name)
    products = products.where("name like ?", "%#{keywords}%") if keywords.present?
    products = products.where(category_id: category_id) if category_id.present?
    products = products.where("price >= ?", min_price) if min_price.present?
    products = products.where("price <= ?", max_price) if max_price.present?
    products.page(params[:page])

The search execute the first, and then filter with the other where condition so you have a 400.000 products search that kill performances, or am I totally (I wish) wrong?

Avatar

In this episode it is defined in initializers/devise.rb.

Avatar
sh
TWITTER_CONSUMER_KEY=... TWITTWR_CONSUMER_SECRET=... rails server

or

sh
export TWITTER_CONSUMER_KEY=...
export TWITTWR_CONSUMER_SECRET=...
rails server
Avatar

Ryan,

It doesn't seem the index updates when you update the data.

Avatar

Thanks Nik! The GoDaddy coupon code for the $12.99 1-year SSL certs from retailmenot.com still worked (2012-07-13). But during checkout, if you extend it from a 1-year cert to more years, the discount applies to each year! I got a 5-year cert for only $64.95! For once, I'm glad GoDaddy is so crappy.

(If you start with the 5-year cert, then it only applies the discount to the first year though).

Avatar

Can anyone help me out in this. I am using dynamic fields to generate the ranges. It all working fine but now I have to put some validation over it, and have to do it on Client side. Now I have spent three valuable days to find out the answer but yet not successful. Is there any way to generate my own predecided id to each dynamic text field instead of current date and time? So that I can do the validation using that Id. Or do any one have any better idea? Any type of help will be appreciated. Thanks in advance.

Avatar

Thank you guys. Both seem promising.

Avatar

I almost fell over too not knowing this. The fact that Rails is reserving a connection I guess part of the 'magic'. I'm wondering now if there's something out I can use to tell Rails not to reserve a connection, or to make controllers explicitly define whether they require a connection...

Avatar

Sorry if this was posted before
but I would recommend adding --skip-active-record after your rails new MyApp command. You will save a lot of troubles.

Best regards.

Avatar

I'm getting the same error, also tried it both ways, no luck. Anybody figured it out?

Avatar

Also, where do you define:

"TWITTER_CONSUMER_KEY" and "TWITTER_CONSUMER_SECRET"

??

Avatar

Ryan, could we use Koala to to the same job for auth and for get user info, right? What your opinion about Koala x omniauth-facebook?
Thanks.

Avatar

checking on one of my apps, it seems like the authentication goes through Twitter's secure connection.

Avatar

Thanks again, Ryan. You say that adding an authentications model is not always necessary to have users authenticate through multiple services. What is the alternative to an authentications model? If there is only one :provider and :uid field, how can you store provider ids and uids from multiple services in the user table?

Thanks.

Avatar

Lets say I had an app on heroku, with omniauth configured to let users sign in with twitter. Would I need an SSL certificate on my site, or is it safe without one?

Avatar

Cool, thanks very much. I will try it later.

Avatar

I am looking for a auto suggest, as a user types in. How do i do that?

Avatar

I have the same issue, did you resolve this?

Avatar

+1 Yes something introducing jRuby would be great for instance I think TorqueBox is great and more people should be looking at it.

Avatar

HOw would you send the message after that? I can generate the form, but I'd like to see the continuation, where he commented out in the controller.
Thought to use NotificationsMailer.new_message(@message).deliver but this gives me error:
undefined methodattribute' for Message:Class`
This may be due to the fact we dont use activeRecord.

Avatar

Thanks for the tip house9!

Unfortunately I've never gotten it to work. I uninstalled the pg gem (I had both 0.13.2 and 0.14.0 installed) and re-installed 0.13.2 with:
sudo gem install pg --version 0.13.2 -- --with-pg-config=/usr/local/Cellar/postgresql/9.1.3/bin/pg_config

to no avail. Still getting "segmentation fault: 11" when I get to the psql prompt with "rails db" or with something like "psql -U [username] [db_name]".

My app requires pg 0.13.2.

Avatar

I've been playing with this setting and have run into a bit of a gotcha.

tl;dr - models don't load in rake tasks/migrations - do this:
config.threadsafe! unless $rails_rake_task or explicitly require the model.

The dependency_loading = false setting in the threadsafe! method means that you won't have access to your models in migrations or rake tasks (ie. db:seed).

It's described here this old rails issue and discussed in this article.

Avatar

For me I had to use the serializer even on a simple model reguardless of it accepting nested attributes. I had a simple table with two fields type and data, where data was hstore and the model had absolutely nothing but validates_presence_of and it would not work until I implemented the serializer, without it, it kept sending it the Raw hash.

Avatar

Just a note (although it's already mentioned in the episode): The code of the voted_for? method is inefficient. The problem is that it actually instantiates an ActiveRecord object which, in this case, is rather unnecessary.

Instead of this

ruby
def voted_for?(haiku)
  evaluations.where(target_type: haiku.class, target_id: haiku.id).present?
end

you should use the exists? method:

ruby
def voted_for?(haiku)
  evaluations.exists?(target_type: haiku.class, target_id: haiku.id)
end

If you are using scopes or something like that, you can also suffix it to a relation:

ruby
def voted_for?(haiku)
  evaluations.where(target_type: haiku.class, target_id: haiku.id).exists?
end

In both cases, rather than instantiating an ActiveRecord object, it would just call the connection's select_value which is way more efficient.

Side note: The exists? and its sibling any? are also helpful for use cases where you want to render a collection or, if it's empty, show the user a message like "No comments yet" or whatever.

Avatar

I've done a mashup of the idea here of sharing templates with railscast 325 but changed to use Handlebars instead of Mustache.

Check it out:

https://github.com/etewiah/sharing_handlebars_templates_in_backbone

I am having a problem though with using exactly the same template on both the client and the server. I explain better in the README. Any comments will be appreciated.

Thanks

Avatar

I've done a mashup of this rails cast with railscast 295 but changed to use Handlebars instead of Mustache.

Check it out:

https://github.com/etewiah/sharing_handlebars_templates_in_backbone

I am having a problem though with using exactly the same template on both the client and the server. I explain better in the README. Any comments will be appreciated.

Thanks

Avatar

That's a great article. Thanks for sharing!

Avatar

This is not an issue specifically with this episode's code. You upgraded from 3.0 to 3.2. Rails already told you what you need to do. Two things:

  1. You did not specify how you would like Rails to report deprecation notices for your development environment, please set config.active_support.deprecation to :log at config/environments/development.rb

  2. You need to remove the debug_rjs config option from your environment files as it was deprecated in 3.1 and removed in 3.2.

Hope this helps.

Avatar

This is just amazing...I found the dataTables plugin a while ago and was trying to figure out all out for a few hours. So I come to railscasts to see if Ryan had anything similar, and lo and behold...he's got an episode on the exact plugin I'm using. This has happened so many times it scary - I'm trying to figure out how to use something and Ryan has an episode on exactly what I'm doing. Railscasts is well worth every dime! Thanks for your awesome work, Ryan.

Avatar

This doesn't work for rails 3.2.5.
I've removed 3.0.0beta from Gemfile and issued bundle install but when I try to run server with:
rails server
I'm getting error:
https://gist.github.com/3098891

Avatar

It's worth noting that sorting things by positive votes - negative votes is not usually what you want to sort by.

Avatar

Is there a reason for not passing the whole user to the delayed method? i.e.
User.share_review(current_user, ...) that way it wouldn't be neccasary to call User.find again with the delayed method?

Avatar

Thanks for saving me a few hours of research to understand the significance of the change.

Avatar

+1
A painful memory while you are exporting 50K above of row in SpreadsheetML
And this occupy one of the worker while in production.
Even you finish export with SpreadsheetML, your customer may can't open it because it is too big and eat up the memory in excel.
I end up writing a rake task and put that in background with spreadsheet gem.
Smaller output and better user experience.
May be I am doing it wrong but that's my experience.