别名列名称为另一个

时间:2015-08-28 10:50:45

标签: ruby-on-rails ruby-on-rails-4 activerecord

我的代码中有这样的东西:

TagsController

@tag = Tag.find(id: Tag.root)

tag.rb

def self.root
  TagHierarchy.select('ancestor_id').where(generations: 0)  
end

问题是:

Couldn't find Tag with 'id'={:id=>#<ActiveRecord::Relation [#<TagHierarchy ancestor_id: 1>]>}

所以我需要将列名ancestor_id别名为id,但我不知道如何...

1 个答案:

答案 0 :(得分:1)

试试这个,可能会帮助你

def self.root
  TagHierarchy.where(generations: 0).first.ancestor_id
end
相关问题