类轨道之间的链接

时间:2016-04-12 14:59:53

标签: ruby-on-rails mongoid

我是铁杆新手,在我的项目中我有两个与他们有关系的班级。问题是他们不能用器官列出服务。以下是我的代码:

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%>

每当我运行它时,这似乎都会返回错误

1 个答案:

答案 0 :(得分:0)

注意:在调用关系方法(例如您的服务中的服务)时,对于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中处理当前文档,然后您需要迭代其中的服务。如果你打电话给姓名(这是一种关系的方法),你将获得关系名称(我相信这就是你所看到的)。

我希望这会有所帮助。我不得不在两行之间做一些猜测,如果你需要任何进一步的帮助,请回复我。