1

I have two models in my app that make use of the Friendly_id gem (it so that the app's URLs are more descriptive and user-friendly.

A Politician Model:

class Politician < ActiveRecord::Base
  has_many :interests
  has_many :issues, through: :interests

  validates :name, :political_party, :title, :slug, presence: true

  extend FriendlyId
  friendly_id :name, use: :slugged
end

and an Issue Model:

class Issue < ActiveRecord::Base
  has_many :interests
  has_many :politicians, through: :interests

  validates :name, :slug, :keywords, presence: true

  extend FriendlyId
  friendly_id :name, use: :slugged
end

Back story: The app wasn't initially pushed to Heroku with the gem installed. I built the app and deployed it to Heroku, and then I came across the Friendly_ID gem. After discovering it, I installed the gem in my Gemfile, tweaked the aforementioned models and controllers, and it works beautifully on my local server.

I thought I was all set. I re-seeded my data on Heroku, and pushed it up, but the changes won't show on the live site. And so instead of using the slug field for each model like so:

www.mysite.com/politicians/barack-obama
www.mysite.com/issues/economy

I still see:

www.mysite.com/politicians/7 (Barack Obama's politician ID number)
www.mysite.com/issues/3 (the ID number for the "Economy" issue)

More Back Story:

I looked at the gem's docs and even a few questions here on StackOverflow, and they all said that if you're adding the gem to a pre-existing app, then to run this code in the Heroku console for each model that the friendly_id gem is applied to:

heroku run console

And then:

Politician.find_each(&:save)
Issue.find_each(&:save)

That didn't work :-/ My app is still not updated with semantic-friendly URLs. I should also add that I initially made use of the gem's helper module. And so, I had:

friendly_id :name, use: [:slugged, :history]

But someone on this StackOverflow thread mentioned to remove the "helper" keyword (and then re-add it after saving?), since that may be preventing it from updating on Heroku. I haven't tried that yet (re-adding the "helper" keyword after saving, but I wanted to put up my question before I wasted another day of trying something, and it not working.

Help please :-)

Community
  • 1
  • 1
Diana E.
  • 309
  • 5
  • 11
  • Did you migrate your heroku db to add the "slug" column? Is everything working in your local :production environment as well as local :development? – eeeeeean Sep 20 '16 at 06:28
  • Yes everything is working perfectly when i run the app locally on the server on my laptop. What's the different between a "local :production environment" vs a "local :development" ? – Diana E. Sep 21 '16 at 03:24
  • And yes I migrate my heroku db by running: heroku run rake db:migrate – Diana E. Sep 21 '16 at 04:35

2 Answers2

0

add it to politician.rb

    def slug=(value) 
      if value.present? 
        write_attribute(:slug, value) 
      end 
    end

and it to redirect from /politicians/7 to /politicians/barack-obama politicians_controller.rb

def set_slug_url
    @item = (*you-model*).friendly.find(params[:id])
    redirect_to action: 'show', id: @item.friendly_id, status: 301 unless @item.friendly_id == params[:id]
  end
Serhii Danovskyi
  • 385
  • 3
  • 17
0

I'm answering my own question -- I figured it out with the help of this Stackover flow thread.

I got it to update on my live Heroku app by making sure my local database version matched up with the database version on Heroku:

To know the local db version do:

$ rake db:version

Then take the version you get locally and make sure you have it in heroku by doing the following:

$ heroku run rake --trace db:migrate VERSION=20151127134901

This takes the database migration to Heroku with "the same version of migration you have locally."

Community
  • 1
  • 1
Diana E.
  • 309
  • 5
  • 11