Рельсы mongoid тримодельные отношения - PullRequest
0 голосов
/ 12 марта 2012

enter image description here

У меня есть та связь, изображенная выше синим цветом, однако она вызывает много ошибок, например нет ошибки метода.

Будет ли встраиваемые отношения (красным) между А и С в порядке? учитывая два других отношения.

Кроме того, в файле маршрута я должен вкладывать B в A?

Пожалуйста, сообщите и спасибо.

class Trip
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name
  key :name

  belongs_to :user
  has_many :logs
end

class Log
  include Mongoid::Document
  field :content
  validates_presence_of :content
  belongs_to :trip
  belongs_to :user
end

class User
  include Mongoid::Document
  has_many :trips
  has_many :logs
end

Ошибка:

NoMethodError in Trips#show

Showing /home/jason/apps/app/views/trips/show.html.haml where line #17 raised:

undefined method `logs_path' for #<#<Class:0x9b8a14c>:0x9d3251c>

Контроллер:

class TripsController < ApplicationController
   def show
    @trip = Trip.find(params[:id])
    @log = Log.new
    @logmsg = @trip.logs
  end
end

class LogsController < ApplicationController

  def create
    @log = @trip.logs.build(params[:log])
    current_user.logs.build(params[:log])

    respond_to do |format|
      if @log.save
        format.html { redirect_to @log, notice: 'successfully created.' }
      else
        format.html { redirect_to trip_path(@trip) }
      end
    end
  end

  def destroy
    I want an ajax destroy here, as the logs will only be shown in the Trips show page.
  end
end

Шоу - поездки / show.html.haml:

= form_for @log do |f|
  .field
    = f.label :content
    = f.text_field :content
  .actions
    = f.submit 'Save'

Ниже приведен отрывок файла маршрута:

App::Application.routes.draw do
  resources :trips do
    resources :logs
  end
end

1 Ответ

2 голосов
/ 12 марта 2012

Нет, вы не можете встроить ModalA в ModalC, потому что ModelB не сможет ссылаться на встроенные объекты.

Встроенные модели на самом деле внутри родительского документа,не в отдельной коллекции.

...