RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Did you ever figure this out? I'm also looking for a non-devise authentication solution for mobile apps.

Avatar

I ended up using:

ruby
def self.open
  joins(:events).where("user_events.id IN (SELECT MAX(id) FROM user_events GROUP BY user_id) AND state = 'open'")
end

Hope that helps.

Avatar

How does this compare with a gem like https://github.com/dsaronin/milia? Is there a multi-tenancy gem that works with Mongoid?

Avatar

Don't forget .includes(:owner, :trackable)

Avatar

Dude, thank you so much for commenting out the collection.fetch({reset: true}). I've been searching hours for this solution. Thank you so much :)

Avatar

how did you get the id of the record in a hidden field? Thanks! I'm using devise and trying to assign an association between a User and an Organization upon registration.

Avatar

Good One. Helped me a lot to get started with implementing authentication as I am a newbie to Ruby. Thanks a lot.

Avatar

Very helpful screencast! There are several Rails 4 helper methods for this and similar form building: https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/form_options_helper.rb

Though doing it using Ryan's way allows for infinite flexibility.

Avatar

Both methods of setting hash values work the same, it's a matter of style preference.

Avatar

Thanks for pointing this out, it helped me get this working in Rails 4.

Avatar

Thanks!!! The url shortener advice helped.

Avatar

Great episode as usual. I love how you test everything. Moving the permissions into a model is definitely the way to go.

Avatar

You can use either one, there is no reason to use one or the other. The key is the readability of your code. Personally, I prefer "if not" because I find it easier to understand.

Avatar

Correct. Ryan has a typo in his code...it should be:

<%= image_tag painting.image.url(:thumb).to_s %>

NOT: <%= image_tag painting.image_url(:thumb).to_s %>

Related Post On Stack Overflow

Avatar

Wonderful episode!

There are some tangential security issues with this, but they are outside the scope (no pun intended) of multitenancy. The most obvious one is any user can create a resource in another scope by simply POSTing a tenant_id other than his own. To prevent this hole, simply make sure the controller does not accept a tenant_id parameter.

Regardless, this is awesome stuff, well thought out, and extremely helpful. Thanks Ryan!

Avatar

Thanks Wayne, really good gem. It's working great for me.

How would I use it when the upload objects are children another object?

I can't see a way to pass any kind of parent_id to the child on creation.

Avatar

I'm using the example at the end of the video that just uses jquery-file-upload to upload direct to S3. It's all working fine but now I want to make the upload a child of a parent object.

I'm uploading files on the parent's new page. How do I pass the parent_id to the child object?

Avatar

It looks like he doesn't support it anymore.

Avatar

Hey, Ryan, we need cancan for Rais 4. Update it, please!

Avatar

Hi Ryan,

How to copy an existing images into Rails project in Rails 2.3.5.
For example I have one item it has some images, I am copying same item but here problem is how to copy existing images.Internally I am using paper clip gem for saving images.

Thanks in advance.

Avatar

I want to use warden in my Sinatra app, but I am lost in one particular thing. How can I call specific route on Sinatra in failure_app lambda? I mean something like SessionsController.action(:new) but with Sinatra app. I know that I can use something like

ruby
[302, {'Location' => '/session/new'}, []]

but this solution don't send env variable, so env['warden'].message can't work.

Avatar

Thanks. What I was trying to do is not possible with Heroku at the moment: Sub-sub domains aren't possible on Heroku default domain (yourappname.herokuapp.com), and you need to use the custom record for that

http://stackoverflow.com/questions/17994288/subdomain-with-rails-and-herokuapp

Avatar

The directory path for store.js.coffee in the Show Notes has the wrong label. It should be in the right place if you generated it. If you placed it there based on the show notes, it might not work.

app/assets/javascripts/models/store.js.coffee
DS.RESTAdapter.configure("plurals", entry: "entries")

It should be

app/assets/javascripts/store.js.coffee
DS.RESTAdapter.configure("plurals", entry: "entries")
Avatar

Can you point me to any resources on how/why/when to do this?

Avatar

Can you expound a bit as to why respond_to isn't all that great?

Avatar

Hmm, nice. I'd like to know how I can keep on using belongs_to though. My tableless model is actually connected to ActiveRecord models, as in episode 193 with articles.

Any new way to do this with rails 4 for instance ?

Avatar

Foundation seems to interfere with TinyMCE 4.x, does anybody experienced that?

Avatar

Here is a presentation that says instead of

WHERE fts @@ to_tsquery('english', 'query')
ORDER BY
ts_rank(fts, to_tsquery('english', 'query')) DESC

use

WHERE fts @@ to_tsquery('english', 'query')
ORDER BY
fts >< to_tsquery('english', 'query')

But the >< operator is not recognized by PostgreSQL? (Version 9.2)

EDIT: ah, later on it says it's going to be a 9.3 feature

Avatar

I rant into this recently and it turned out that Webrick was the culprit. On every request it appears Webrick was trying to do DNS reverse resolution. Our solution was to switch it to using thin but you can also modify webrick/config.rb.

Avatar

Just want to say how great this addition has been to a project! I am doing alot of work with a SOAP API and when things go wrong the "live shell" has been awesome.

Avatar

If you are using a FormBuilder, maybe you can prefer use this for generate the checkbox name. For example, if we have a form with nested forms for a has_many association and each of these need a category_ids collection of checkboxes calculate the name of these checkboxes can be a problem. So, I suggest use the object_name method of the FormBuilder object:

ruby
<%= hidden_field_tag "#{f.object_name}[category_ids][]", nil %>
<% Category.order(:name).each do |category| %>
  <div>
    <%= check_box_tag "#{f.object_name}[category_ids][]", category.id, f.object.categories.include?(category) %>
    <%= category.name %>
  </div>
<% end %>

Note that I use f.object instead of @product too. I think this way is better for use with a FormBuilder.

Finally, as you can see, I use an input hidden for be sure always send a category_ids array, so, I don't need setup this field in the controller.

I hope you find it useful :)

Avatar

Anybody got an idea how to generate a histogram in PDF?

Avatar

Chart is not shown in pdf file generated by pdfkit gem..... anyone who solve this problem??

thanks.

Avatar

I'm using Rails 4, and after trying to import other files after a successful setup of foundation I'm still getting an error with the mixings... "File to import not found or unreadable: foundation/variables." whenever I add "@import "foundation/variables";" to any of the sass style files

Avatar

I'm having the exact same problem. Did anyone find a solution?

Avatar

This worked for me...

CoffeeScript
@testApp.config ["$httpProvider", ($httpProvider) ->
  token = $('meta[name=csrf-token]').attr('content')
  $httpProvider.defaults.headers.common['X-CSRF-Token'] = token
]
Avatar

Does anyone know how to get this working with nested routes? I'm having a lot of issues in my link_to code

Avatar

The gem is outdated for rails 4. Better try for an alternative

Avatar

not sure if this is relevant but did you set heroku up to use subdomains such as ***.myapp.com. You can set it up by using wildcard domains as per herokus documentation

https://devcenter.heroku.com/articles/custom-domains#wildcard-domains

Avatar

I think you need to add
attr_accessor :skill_ids to it.

Avatar

Nice railscast! One of my favorites. Your site is the best. I am using this webcast as the basis for deploying servers. Thanks again

Avatar

If you'r planning on using this plugin for the sole purpose of multiple uploads you may want to hold off for awhile. this plugin supports all modern browsers and IE10 up but IE8 and IE9 still total about 25% of browser usage across the globe.

Stats: http://gs.statcounter.com/#browser_version-ww-monthly-201207-201307-bar

Avatar

i had same trouble in rspec-rails 2.14.0.

Because of rspec-mocks syntax changed. 'allow' matcher can't use.

Only rename :allow to :other_name, then the code works well.

Avatar

Is named_scope deprecated now in Rails 4.0? 'scope' still seems to work though