0

This is a method I'm using to slug the title of an article. The problem is I'm getting an error with any title that has a "." followed or preceded letters. For example "example.com", "U.S." doesn't match. However it works when using numbers.

  def self.to_slug(str)
    #strip the string
    ret = str.strip

    ret.gsub! /['`]/,"-"


    ret.gsub! /\s*@\s*/, " at "
    ret.gsub! /\s*&amp\s*/, " and "
    ret.gsub! /\s*&\s*/, " and "
    ret.gsub! /\s*[.]\s*/, "-"

    ret.gsub! /\s*[^A-Za-z0-9\.\-]\s*/, '-'  

    ret.gsub! /-+/,"-"

    ret.gsub! /\A[-\.]+|[-\.]+\z/,""
    ret = ret.downcase
    ret
  end

this is the title: Guitar legends Fender snap up Irish music start-up for 5m - Independent.ie.

The slug it makes is guitar-legends-fender-snap-up-irish-music-start-up-for-5m-independent-ie and this is the error:

Mysql2::Error: Column 'id' in where clause is ambiguous: SELECT  `articles`.`id` AS t0_r0, `articles`.`feed_id` AS t0_r1, `articles`.`publish_date` AS t0_r2, `articles`.`url` AS t0_r3, `articles`.`name` AS t0_r4, `articles`.`author` AS t0_r5, `articles`.`image` AS t0_r6, `articles`.`image_caption` AS t0_r7, `articles`.`text` AS t0_r8, `articles`.`guid` AS t0_r9, `articles`.`slug` AS t0_r10, `articles`.`video_url` AS t0_r11, `articles`.`active` AS t0_r12, `articles`.`created_at` AS t0_r13, `articles`.`updated_at` AS t0_r14,
user3213561
  • 359
  • 1
  • 4
  • 15

1 Answers1

0

If you want to convert:

Guitar legends Fender snap up Irish music start-up for 5m - Independent.ie

To

Guitar-legends-fender-snap-up-irish-music-start-up-for-5m-independent-ie 

You have just to use this regex with - as replacement string:

\W+

Working demo

Federico Piazza
  • 30,085
  • 15
  • 87
  • 123