Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 类方法与数据库字段上的Rails优先级_Ruby On Rails - Fatal编程技术网

Ruby on rails 类方法与数据库字段上的Rails优先级

Ruby on rails 类方法与数据库字段上的Rails优先级,ruby-on-rails,Ruby On Rails,我有一个Picture类,我想根据一些逻辑重写返回“description”数据库列值 这在Rails中有效吗?在返回数据库列值之前,我是否可以始终依靠类调用类方法 # database columns for Picture class # description => String # parent_id => Integer class Picture < ActiveRecord::Base def description if parent_id.ni

我有一个Picture类,我想根据一些逻辑重写返回“description”数据库列值

这在Rails中有效吗?在返回数据库列值之前,我是否可以始终依靠类调用类方法

# database columns for Picture class
# description => String
# parent_id => Integer

class Picture < ActiveRecord::Base

  def description
    if parent_id.nil?
      self.description      
    else
      description = Picture.find(parent_id).description
    end
  end

end 

我想我在这里试图变得不必要的复杂

不完全清楚您想做什么,但您可以使用super从方法中获取描述值:

def description
  if parent_id.nil?
    super
  else
    self.description = Picture.find(parent_id).description
  end
end

注意,我将最后一行更改为使用
self.description=
,因为我假设您要设置description值。如果情况并非如此,则不需要将其分配给任何变量。

不完全清楚您试图执行的操作,但您可以使用super从方法中获取描述值:

def description
  if parent_id.nil?
    super
  else
    self.description = Picture.find(parent_id).description
  end
end

注意,我将最后一行更改为使用
self.description=
,因为我假设您要设置description值。如果不是这样,则不需要将其分配给任何变量。

您应该在描述方法中使用read_属性(:description)(可以重写)

此外,您可以在else部分中执行parent.description,而不是Picture.find rigmarole。使用家长协会,我相信你有

def description
  # if parent_id is there and the parent object can be found and that parent has a description
  if (parent_id? && parent && parent.description?)
    # show parental description
    parent.description
  else
    # read my own database attribute
    read_attribute(:description)
  end
end

不确定分配给上面的描述是否有用,因为它最终将成为一个局部变量。在reader中执行self.description=只是有点讨厌而已。

您应该在描述方法中使用read\u属性(:description)(可以重写)

此外,您可以在else部分中执行parent.description,而不是Picture.find rigmarole。使用家长协会,我相信你有

def description
  # if parent_id is there and the parent object can be found and that parent has a description
  if (parent_id? && parent && parent.description?)
    # show parental description
    parent.description
  else
    # read my own database attribute
    read_attribute(:description)
  end
end

不确定分配给上面的描述是否有用,因为它最终将成为一个局部变量。在reader中执行self.description=只是有点讨厌而已。

我试图返回图片的描述,这取决于它是子图片还是父图片(它们可以嵌套)。这个例子有点做作……我可以通过使用一个与数据库列没有相同名称的方法轻松避免这种情况……例如def my_description if parent_id.nil?#返回数据库数据,因为没有父级self.description else#如果存在父级的描述,则返回父级的描述description=Picture.find(parent_id)。description end end我想我在尝试不必要的复杂化,请参见原始问题中的edit。我们尝试使用与数据库列同名的方法,但这似乎只是使事情变得不必要的复杂化。您可以使用super吗?我认为如果已经定义了一个名为same的方法,Rails就不会定义属性访问器?刚刚在我自己的项目中尝试过,似乎你是对的。酷:)我试图返回图片的描述,这取决于它是子图片还是父图片(它们可以嵌套)。这个例子有点做作……我可以通过使用一个与数据库列没有相同名称的方法轻松避免这种情况……例如def my_description if parent_id.nil?#返回数据库数据,因为没有父级self.description else#如果存在父级的描述,则返回父级的描述description=Picture.find(parent_id)。description end end我想我在尝试不必要的复杂化,请参见原始问题中的edit。我们尝试使用与数据库列同名的方法,但这似乎只是使事情变得不必要的复杂化。您可以使用super吗?我认为如果已经定义了一个名为same的方法,Rails就不会定义属性访问器?刚刚在我自己的项目中尝试过,似乎你是对的。酷:)酷。看起来read_属性(:description)的工作原理与Beerlington关于调用“super”的建议相同。谢谢你的帮助。看起来read_属性(:description)的工作原理与Beerlington关于调用“super”的建议相同。谢谢你的帮助