Thomas Bush


4,680 Experience
22 Lessons Completed
2 Questions Solved


Thanks Chris! A tutorial on this would be awesome - everything I find is so confusing. I have wanted to do this exact same thing for a personal project for a while now, but hadn't had the time to look into it in depth. This project for work is just forcing the issue.

Just to make sure I am understanding this flow correctly:

  • Go to
  • user is redirected to
  • authenticate
  • redirected back to

Why I ask specifically is changing domains throws off our analytics, namely Marketo. This is why I was hoping to avoid such a solution initially. If this is unavoidable than so be it. Just wan to make sure I can explain this correctly to the analytics guys/my boss.

For a rails/devise project I'm trying to figure out how to have the same devise login (email/password) work for multiple apps on different domains. I can't figure out if this would that be considered "Single Sign On” or “Remote Authenticatable”?

I want this to be seamless for example:

Thanks Chris, this really help!

I have case studies and site models in my database. I have a has and belongs to many association set up between them.


When on a site#show page I want to write the following erb

<% @site.case_studies.order("position").each do |case_study| %>

The result is the correct case studies, but instead gives the case_studies table position, as opposed to the case_studies_sites table position which I would expect/need. How would I get the joint table's position attribute instead of the Model's position attribute. Does this make sense what I am asking?



Posted in How do I allow a multi-select to be sortable?

I have a ton of many-to-many relationships in a CMS I built for work. I am currently using Select2 3.5.3 to assign these relationships. This all works well. My issue is attempting the make them drag and drop sortable and store the result of this in the database.

I got the Select2 Drag and Drop Sorting section demo working in my forms, but I don't quite understand how I would make this dynamic like the rest of my select2 instances -- in my simple implementation I initialize select2 on anything with the .select2 class. I also don't understand how I would store order in the db.

I have an example below modified from an old Ryan Bates update function. In this example I am attempting to update the Case Study model relationship with the Site model. Case Study and Site have standard has-and-belongs-to-many relationships with each other.



  has_many :case_studies_sites
  has_many :case_studies, :through => :case_studies_sites


  has_many :case_studies_sites
  has_many :sites, :through => :case_studies_sites

Update Function I think it would be accomplished. One other important point to note: I have added a position:integer field to the case_studies_sites table.

  def select2_sort
    self.case_studies.each_with_index do |id, index|
      CaseStudiesSite.where(case_study_id: id).update_all({position: index+1})

Forms and JS


Here is an example of the sites form.

   <div class="form-group">
      <%= f.label :case_study, "Related Case Studies" %>
      <%= f.collection_select :case_study_ids, CaseStudy.all, :id, :title, {}, {class: 'select2 form-control', multiple: true} %>

And finally here is the coffee script I am currently using to run the basic select2.

$(document).on 'ready page:load', ->

Both the form fields and JS are currently set for the standards select2 (aka not sortable) as I have not figured out how to translate the select2 demo to these fields.

Posted in Deploy Ubuntu 14.04 Trusty Tahr Discussion

This is caused by the lack of a secret_key_base export.

Posted in Advanced routing setup

Yes, that helps. Yes the lack of tire is really frustrating as I have to ignore about 85% of what I find as it includes tire gem.

I confirmed that benefits is in fact included in in the product record. Using the syntax below in case it may be useful to anyone else.

curl -XGET ''

I get a listing of products like the example included below:

  "name":"product name"
    {"id":2,"name":"my benefit 2"},
    {"id":6,"name":"my benefit 6"},
    {"id":7,"name":"my benefit 7"}
    {"id":2,"name":"category 2"}

Now I think I might just have a syntax issue. I need to figure out how to search for products with benefits with ids 2, 6, AND 7 in ElasticSearch if I wanted the above example product.

Posted in Advanced routing setup

So you might hate me... this worked, but I needed to make a few changes. The products section of our site receives the majority of our traffic, so I am making an effort to make it as fast as possible. With this is mind, I moved to elasticsearch searches over active record in an attempt to increase speed and flexibility of this page.

I needed to add the ability to search for products that fit into various Categories, Benefits, Collections, and Families as well as the initial Industries and Technologies requirements. We need clean urls to contain dash separated keywords which needed to be unique across previously mentioned list of models.

To solve I made a polymorphic model containing a string :identity and included in all above models. Now I can verify uniqueness of the slug across models. I can also get results by searching the polymorphic model. I can currently pull the identities from my url, process them to figure out which industries, collections, families, etc apply.

I am just having trouble submitting to ElasticSearch to get results. Every tutorial I find mentions the Tire gem which is no more. ElasticSearch-rails gem has a few examples, but the jump from the basic example to the expert example is so great that I am having trouble following. I can currently search my products for matching collections or families as product contains a foreign key for these. My trouble is when I try to search for Products with related Benefits.

Sorry, I think this is a lot to ask and I am not sure I did a great job laying out my specific confusion, but any help or direction you could provide would be appreciated!

Posted in Advanced routing setup

This basic concept helped me get setup however I am having a bit more trouble.

declaring find_products_if_not_found as a before filter never gets called as the set_product throws a record not found exception. To solve this I removed this before filter and added a rescue to the set_product filter.

    def set_product
      @product = Product.friendly.find(params[:id])
      rescue ActiveRecord::RecordNotFound

If I pass in an id associated with a technology this works correctly, however industries won't work. The OR clause is never executed as again you get a record not found exception. Any ideas? Thanks!

Posted in Advanced routing setup

I recently watched the look into routing video. I have a very complex routing scenario I have been asked to set up for work. I was hoping I could lay it out and get some help.

Currently we have:

  • products
  • technologies
  • industries

I would like to be able to use the following url structure

  • products
  • products/:slug
  • products/:technology_name - show all products belonging to that technology
    • products/:industry_name - show products belonging to that industry

The first two parts of these are understood and completed. I currently have a product listing page as well as use friendly_id so I can have clear product urls mapping to detail pages.

I have a basic concept of how I would code the controller to find the technology id by the technology name, return results from the products_technologies relationship table with the correct technology id.

My confusion is how to handle this from a routing perspective. This also conflicts with friendly_id, and I assume would conflict with industries as well. I would really appreciate any help or information you could provide. Thanks!

Posted in Fragment Caching And oEmbed Discussion

Even better, syncing those two, expiration and cache, was bothering me. Thanks Chris!

Posted in Fragment Caching And oEmbed Discussion

Really great episode, I will be implementing something similar soon. While watching I had a question: Could the first-time-slow-page be avoided by making a rake task to visit all video pages using something like httparty, watir, mechanize, or something similar? We have a little over 200 videos on our site, so this would be nice to avoid the delay every day/week/whatever we set the cache length.

Posted in Nginx.conf failed

Figure this out, figured I should post in case someone has similar issues. Looking at the logs as you mentioned led me to the answer. The error log showed the following:

```015/01/14 08:50:38 [error] 18547#0: *1 directory index of "/home/deploy/myApp/current/public/" is forbidden, client:, server:, request: "GET / HTTP/1.1", host: ""

I had to do a couple things:
* change permissions `chmod -R 775 /var/log/nginx/`
* change ownership `chown -R deploy /var/log/nginx/`
* use `sudo service nginx start` to start the server instead of `service nginx start`

Thanks for the help Chris, I was really struggling with this.

Posted in Nginx.conf failed

I am having similar issues and was unable to solve by following this thread. I am also using rvm, and ruby 2.1.5.

The path to my ruby
```which ruby

Tests seem to pass

```sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

However restarting nginx fails
```service nginx restart

  • Restarting nginx nginx [fail] ```

I have my /etc/nginx/nginx.conf set up for passenger below

```# Phusion Passenger config
# Uncomment it if you installed passenger or passenger-enterprise
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
# passenger_ruby /home/deploy/.rvm/rubies/ruby-2.1.5/bin/ruby;
passenger_ruby /home/deploy/.rvm/wrappers/ruby-2.1.5/ruby;

if I uncomment the first 'passenger_ruby' and run 'sudo nginx -t' 

```nginx: [emerg] "passenger_ruby" directive is duplicate in /etc/nginx/nginx.conf:65
nginx: configuration file /etc/nginx/nginx.conf test failed

When I visit the site in a browser I get a 403 forbidden. Sorry this is a lot but I have been struggling with this for a while so I wanted to provide any information I thought may be useful to help solve this. Thanks for any help you can provide!

Posted in Likes routing error

Regenerating the table with ID fixed this issue. THANKS! I could have sworn I read somewhere that was the best way to create join tables, clearly it causes issue though.

Also for clarity, my model is favorite, the "s" was a typo.

Thanks again!!! Really appreciate it. I really enjoy your tutorials, please keep them coming!

Posted in Likes routing error

Chris, thanks for the quick replies!

I open rails console and verified a favorite. Checked my user id, checked the location id, verified that location.first.favorites works showing the first user having a favorite of the first location.

The interesting part is that I cannot delete a favorite in the console. I figured maybe I set up wrong originally so I Would delete all favorites and start fresh. Inside console:


lists my favorites, but


throws the exact same error as the site

SQLite3::SQLException: no such column: favorites.: DELETE FROM "favorites" WHERE "favorites"."" = ?

Would you mind double checking my relationships included in the initial post, this relationship setup is something I have never used before, specifically the "source" portions. Google searches turned up a lot of solutions resulting in correcting relationship setup.

Also here is my *schema.rb. Figured this may be beneficial as the last error would make me think its some sort of database issue. This seems very straight forward to me though.

  create_table "favorites", id: false, force: true do |t|
    t.integer "user_id"
    t.integer "location_id"

  add_index "favorites", ["location_id"], name: "index_favorites_on_location_id"
  add_index "favorites", ["user_id"], name: "index_favorites_on_user_id"

Also final thought: Where should the favorite model be located? This route setup where using favorites as a module is also new for me. Currently, this file is located in app/models/favorites should this be inside a locations folder?

Thanks I really appreciate your help especially considering its a weekend.

Posted in Likes routing error

Here is favorites#destroy

  def destroy

    respond_to do |format|
      format.html {redirect_to store_location_path(@store, @location)}

Posted in Likes routing error

I am trying to use the Likes tutorial to create Favs for locations, which are nested under stores.

Here is the relevant route section

resources :stores do
    resources :locations do
      resource :favorite, module: :locations

It should be noted I am using Friendly ID and that works for my stores and locations currently.


Favoriting works however un-fav throws the following error.

SQLite3::SQLException: no such column: favorites.: DELETE FROM "favorites" WHERE "favorites"."" = ?



  has_many :favorites
  has_many :users, :through => :favorites, :source => :user


  has_many :favorites
  has_many :locations, :through => :favorites, :source => :location

I would real appreciate any help you could provide.

logo Created with Sketch.

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

© 2022 GoRails, LLC. All rights reserved.