【问题标题】:Alias column name to another别名列名到另一个
【发布时间】:2015-08-28 10:50:44
【问题描述】:

我的代码中有这样的内容:

标签控制器

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

标签.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 但我不知道如何..

【问题讨论】:

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


    【解决方案1】:

    试试这个,也许对你有帮助

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

    【讨论】:

      猜你喜欢
      • 2022-12-11
      • 2016-09-09
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      • 2016-11-16
      • 1970-01-01
      • 2022-01-10
      • 2011-11-07
      相关资源
      最近更新 更多