Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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 如何基于sql结果数据显示结果_Ruby On Rails_Ruby On Rails 4_Model View Controller - Fatal编程技术网

Ruby on rails 如何基于sql结果数据显示结果

Ruby on rails 如何基于sql结果数据显示结果,ruby-on-rails,ruby-on-rails-4,model-view-controller,Ruby On Rails,Ruby On Rails 4,Model View Controller,我在我的新网站中使用了闭包树的概念。 在其中一个show视图中,我希望通过id(1级子体)选择数据,或者如果id为null,则选择第一级。 如何将sql与set result连接 查询: select id,name from tags t join tag_hierarchies th (t.id = th.ancestor_id) where t.id=nvl(?,0) and th.generations=1 到目前为止的代码(app/views/show.erb上的问题): d

我在我的新网站中使用了闭包树的概念。 在其中一个show视图中,我希望通过id(1级子体)选择数据,或者如果id为null,则选择第一级。 如何将sql与set result连接

查询:

select id,name 
from tags t 
join tag_hierarchies th (t.id = th.ancestor_id) 
where t.id=nvl(?,0) and th.generations=1 
到目前为止的代码(app/views/show.erb上的问题):

db/schema.rb:

  create_table "tags" do |t|
    t.string "name",  :null=>false
    t.boolean "isCat", :default => true
  end

  create_table "tag_hierarchies", :id => false do |t|
    t.integer "ancestor_id", :null => true
    t.integer "descendant_id", :null => false
    t.integer "generations", :null => false
  end

  add_foreign_key(:tag_hierarchies, :tags, :column => 'ancestor_id')
  add_foreign_key(:tag_hierarchies, :tags, :column => 'descendant_id')
app/models/tag.rb

class Tag < ActiveRecord::Base
  #attr_accessible :name, :isCat

  validates :name, uniqueness: false, allow_blank: false
end
class标记
app/models/Tag_hierarchy.rb

class TagHierarchie < ActiveRecord::Base
  #attr_accessible :ancestor_id, :descendant_id, :generations
end
类标记层次结构 app/views/show.erb

<% provide(:title, category_name_or_constant(@tags)) %>
<h1><%= category_name_or_constant(@tags)%></h1>

<div class="row">
  <div class="span6 offset3">
    <%= for(<<here goes the sql by the Closure tree >>) do |f| %>
        <%= link_to tag.name, tag %>
    <% end %>
  </div>
</div>

将静态方法(或范围,如果您愿意)添加到标记模型:

app/models/tag_hierarchy.rb

class TagHierarchy
  belongs_to :tag, foreign_key: :ancestor_id
end
class Tag
  has_many :tag_hierarchies, foreign_key: :ancestor_id

  def self.descendants(id = nil)
    id ||= 0
    self.where(id: id).joins(:tag_hierarchies).where(tag_hierarchies: {generations: 1})
  end
end
class TagsController < ApplicationController
  def index
    @descendants = Tag.descendants
  end

  def show
    @descendants = Tag.descendants(params[:id])
  end
end
app/models/tag.rb

class TagHierarchy
  belongs_to :tag, foreign_key: :ancestor_id
end
class Tag
  has_many :tag_hierarchies, foreign_key: :ancestor_id

  def self.descendants(id = nil)
    id ||= 0
    self.where(id: id).joins(:tag_hierarchies).where(tag_hierarchies: {generations: 1})
  end
end
class TagsController < ApplicationController
  def index
    @descendants = Tag.descendants
  end

  def show
    @descendants = Tag.descendants(params[:id])
  end
end
创建控制器:

rake g控制器标记控制器

向控制器添加代码:

app/controllers/tags\u controller.rb

class TagHierarchy
  belongs_to :tag, foreign_key: :ancestor_id
end
class Tag
  has_many :tag_hierarchies, foreign_key: :ancestor_id

  def self.descendants(id = nil)
    id ||= 0
    self.where(id: id).joins(:tag_hierarchies).where(tag_hierarchies: {generations: 1})
  end
end
class TagsController < ApplicationController
  def index
    @descendants = Tag.descendants
  end

  def show
    @descendants = Tag.descendants(params[:id])
  end
end
class标记控制器
然后使用视图中的所有内容:

app/views/tags/show.html.erb

<div class="row">
  <div class="span6 offset3">
    <%= @descendants.each do |tag| %>
        <%= link_to tag.name, tag %>
        <%# you can also use tag.tag_hierarchies here %>
    <% end %>
  </div>
</div>

我建议您在Rails上阅读一些教程和/或文档: