父资源以及子资源

时间:2015-02-11 02:07:11

标签: ruby-on-rails model-view-controller

我想弄清楚我应该以哪种方式配置我的模型/控制器。该模型如下:

class Example < ActiveRecord::Base
  belongs_to :creator, :class_name => "User"

  belongs_to :group <--- this is where I have the problem.

  has_many :people, :through => :exampleships
  has_many :exampleships, :foreign_key => "other_example_id"

我希望Example既有自己的索引/创建,也可以在组index / create下显示为嵌套资源。但由于某种原因,我无法让它发挥作用。

我的控制器如下:

    class ExamplesController < ApplicationController
  before_action :set_event, only: [:show, :edit, :update, :destroy]
  before_filter :check_user, only: [:edit, :update, :destroy]

  # GET /examples
  # GET /examples.json
  def index
    @examples = Example.all.order("created_at DESC")
  end

  # GET /examples/1
  # GET /examples/1.json
  def show
    @event = Example.find(params[:id])
  end

  # GET /examples/new
  def new
    @event = Example.new(params[:group_id => :group_id])
  end

  # GET /examples/1/edit
  def edit
  end

  # POST /examples
  # POST /examples.json
  def create
    @group = Group.find(params[:group_id])
    @example = current_user.examples.new(example_params)
    @example.people << current_user

但后来我收到的错误是无法找到没有ID的群组

这是我的小组模型的一个例子:

belongs_to :creator, :class_name => "User"

  has_many :members, :through => :memberships
  has_many :memberships, :foreign_key => "new_group_id"

  has_many :examples, :through => :members

0 个答案:

没有答案