从模型访问属性?

时间:2011-11-14 07:34:45

标签: ruby-on-rails ruby

我有“预订”模型和列:titlepermalinkauthornamedescription

before_save :change_permalink
before_update :change_permalink

private

  def change_permalink
    self.permalink = operation_with_title(title)
  end

  def operation_with_title(string)
    mytitle = string.downcase!
    mytitle.delete!(".,?!()@#\$&*^%-+=/[]{}<>`~''\"")
    new_string = []
    mytitle.each_char do |c|
      if translit.key? c
        new_string << translit[c]
      else
        new_string << c
      end
    end
    new_string.to_s
  end

  def translit
    {
      "а" => "a", "б" => "b", "в" => "v",
      "г" => "g", "д" => "d", "е" => "e",
      "ё" => "jo", "ж" => "zh", "з" => "z",
      "и" => "i", "й" => "ij", "к" => "k",
      "л" => "l", "м" => "m", "н" => "n",
      "о" => "o", "п" => "p", "р" => "r",
      "с" => "s", "т" => "t", "у" => "u",
      "ф" => "f", "х" => "h", "ц" => "c",
      "ч" => "ch", "ш" => "sh", "щ" => "xh",
      "ь" => "", "ы" => "y", "ъ" => "",
      "э" => "je", "ю" => "ju", "я" => "ja",
      " " => "_"
    }
  end

我想访问title,对其进行更改并添加到permalink

但我不知道如何在我的方法中访问title

1 个答案:

答案 0 :(得分:0)

title应与self.title

一起使用

你能告诉我们你的尝试以及它给你的错误吗?