【问题标题】:Links between class rails类轨道之间的链接
【发布时间】:2016-04-12 14:59:52
【问题描述】:

我是 Rails 新手,在我的项目中,我有 2 个班级,他们之间有关系。问题是他们不能列出器官服务。以下是我的代码:

modelos

class Admin::Organ
  include Mongoid::Document
  include Mongoid::Search
  include Mongoid::Pagination

  field :name, type: String
  field :address, type: String
  field :acronym, type: String
  field :phones, type: String
  field :emails, type: String
  field :image, type: String
  field :permalink, type: String
  field :schedules, type: Array
  field :coordinates, type: Hash

  has_many :services, class_name: "Service"
  has_many :units, class_name: "Admin::Unit"

  before_save :touch_permalink

  search_in :name

  paginates_per 10

  def url
    "/orgao/#{permalink}"
  end

  private
    def touch_permalink
      self.permalink = self.name.parameterize
    end
end



class Service
    include Mongoid::Document
    include Mongoid::Search
    include Mongoid::Pagination

    field :name, type: String
    field :acronym, type: String
    field :popular_names, type: Array
    field :description, type: String
    field :free, type: Boolean
    field :applicants, type: Array
    field :estimated_time, type: Hash
    field :steps, type: Array
    field :permalink, type: String
    field :other_informations, type: String

    belongs_to :organ, class_name: "Admin::Organ"
    has_and_belongs_to_many :units, class_name: "Admin::Unit"
    has_and_belongs_to_many :audiences, class_name: "Admin::Audience"
    has_and_belongs_to_many :categories, class_name: "Admin::Category"

    before_save :touch_permalink

    search_in :name, :popular_names

    paginates_per 10

    def organ_id 
      read_attribute(:organ_id).to_s
    end

    def url
        "/servico/#{permalink}"
    end

    private
      def touch_permalink
        self.permalink = self.name.parameterize
      end
end


#controlers
class   ServicesController < ApplicationController
    def index
        @organs = Admin::Organ.all
        @services =  Service.page(params[:page].to_i).per(3)
    end

    def show
        @service = Service.where(permalink: params[:permalink]).first

    end
end


class   OrgansController < ApplicationController
    def index
        @organs = Admin::Organ.page(params[:page]).per(2)       
    end

    def show
        @organ = Admin::Organ.where(permalink: params[:permalink]).first
        @organs = Admin::Organ.page(params[:page]).per(1)
    end
end

#call in index.html 
<%= organs.services.name%>

这似乎在我运行时返回错误

【问题讨论】:

  • 请显示堆栈跟踪。
  • 您能否添加有关您使用此方法遇到的特定错误的任何信息
  • 这将返回名称“服务”!我想要谁返回服务的名称。

标签: ruby-on-rails mongoid


【解决方案1】:

注意:在 mongo 关系上,当您调用关系方法(例如您的案例中的服务)时,会返回与其关联的所有文档。同样对于您正在使用的分页插件(我假设语法上是 kaminari),这也将返回一个文档集合。

如果是这样,您可能会看到器官视图有点像这样:

<% @organs.each do |organ| %>
   <div>Name: <%= organ.name %></div>
   <div>Address: <%= organ.address %></div>
   <div>Services:</div>
   <% organ.services.each do |service| %>
       <div>Name: <%= service.name %></div>
       <div>Acronym: <%= service.acronym %></div>
   <% end %>
<% end %>

<%= paginate @organs %>

您会注意到,要打印出您需要首先处理@organs 中的当前文档的服务,然后您需要迭代其中的服务。如果您要调用 name(这是关系的一种方法),您将获得关系名称(我相信这就是您所看到的)。

我希望这会有所帮助。我不得不在字里行间做一些猜测,如果您需要任何进一步的帮助,请回来找我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    相关资源
    最近更新 更多