简体   繁体   English

Rails虚拟属性未更新

[英]Rails Virtual Attributes Not Updating

I have a Model "Tag" that links to the dbpedia article for a particular topic. 我有一个链接到dbpedia文章的特定主题的“标签”模型。 I want to create a virtual attribute that will use this dbpedia URI to generate a wikipedia URI. 我想创建一个虚拟属性,它将使用此dbpedia URI生成一个Wikipedia URI。 Here's roughly what the class looks like: 该类大致如下所示:

class Tag < ActiveRecord::Base

  # Validation
  validates_presence_of :dbpedia_uri, :label

  #Virtual Attribute
  def wikipedia_uri
    "#{dbpedia_uri}".gsub("dbpedia.org/resource", "wikipedia.org/wiki")    
  end

end

When I go into my console and try to check Tag.all[1].wikipedia_uri, I get the following error: 当我进入控制台并尝试检查Tag.all [1] .wikipedia_uri时,出现以下错误:

NoMethodError: undefined method `wikipedia_uri' for #<Tag:0xa0c79ac>

Additionally, I have a second Model (Map) with virtual attributes that do work, but when I change them, I still get the old value. 此外,我还有第二个具有有效工作的虚拟属性的模型(地图),但是当我更改它们时,我仍然可以得到旧值。 So when the code is: 因此,当代码为:

 def thumbnail_uri
    "#{map_base_uri}/thumbnails/#{identifier}.jpg"
  end

Map.all[1].thumbnail_uri returns something like: "http://WEBSITE/maps/thumbnails/g3201b.ct002662.jpg" Map.all [1] .thumbnail_uri返回的内容类似于:“ http://WEBSITE/maps/thumbnails/g3201b.ct002662.jpg”

and when I change the code to: 当我将代码更改为:

def thumbnail_uri
   "test"
end

I still get the output: "http://WEBSITE/maps/thumbnails/g3201b.ct002662.jpg" 我仍然得到输出:“ http://WEBSITE/maps/thumbnails/g3201b.ct002662.jpg”

I've tried reloading my Rails Console, but it still doesn't seem to be updating the virtual attributes. 我曾尝试重新加载Rails Console,但它似乎仍未更新虚拟属性。 Any thoughts on what could be causing this issue? 有什么想法可能导致此问题?

Figured out the issue. 找出问题。 I was stupidly running my console in a directory for a duplicate backup set of files, so all of the methods and variables from the backup were available in the console, but my changes to the current code weren't affecting it 我愚蠢地在一个目录中运行控制台以获取一组重复的文件备份,因此备份中的所有方法和变量都可以在控制台中使用,但是对当前代码的更改并没有影响它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM