0

私のアプリの基本的な概要。現在2機種あります。ジョブ モデルとクライアント モデル。has_and_belongs_to_manyユーザーがクライアント エントリを作成し、それらに 1 つまたは複数のジョブを割り当てられるようにすることを意図したため、両方のモデルには関係があります。

ここに私のモデルの両方があります。

クライアント -

class Client < ActiveRecord::Base 
  has_and_belongs_to_many :job
end

ジョブ -

class Job < ActiveRecord::Base
  has_and_belongs_to_many :client
end

私はいくつかの調査を行ってきましたが、関係が機能するには外部キーが必要であると考えるのは正しいと思うので、データベースに列と列client_idを追加しました。job_id

クライアントページは現在機能しており、これがそのためのコントローラーです。

class ClientsController < ApplicationController

        def index
          @clients = Client.all

          respond_to do |format|
            format.html # index.html.erb
            format.json { render json: @clients }
          end
        end

        # GET /Clients/1
        # GET /Clients/1.json
        def show
          @clients = Client.find(params[:id])

          respond_to do |format|
            format.html # show.html.erb
            format.json { render json: @clients }
          end
        end

        # GET /Clients/new
        # GET /Clients/new.json
        def new
          @clients = Client.new

          respond_to do |format|
            format.html # new.html.erb
            format.json { render json: @clients }
          end
        end

        # GET /Clients/1/edit
        def edit
          @clients = Client.find(params[:id])
        end


        def create
          @clients = Client.new(params[:client])

          respond_to do |format|
            if @clients.save
              format.html { redirect_to @clients, notice: 'Client was successfully created.' }
              format.json { render json: @clients, status: :created, location: @clients }
            else
              format.html { render action: "new" }
              format.json { render json: @clients.errors, status: :unprocessable_entity }
            end
          end
        end

        # PUT /Clients/1
        # PUT /Clients/1.json
        def update
          @clients = Client.find(params[:id])

          respond_to do |format|
            if @clients.update_attributes(params[:client])
              format.html { redirect_to @clients, notice: 'Client was successfully updated.' }
              format.json { head :no_content }
            else
              format.html { render action: "edit" }
              format.json { render json: @clients.errors, status: :unprocessable_entity }
            end
          end
        end

        # DELETE /Clients/1
        # DELETE /Clients/1.json
        def destroy
          @clients = Client.find(params[:id])
          @clients.destroy

          respond_to do |format|
            format.html { redirect_to :clients , notice: 'Client was successfully removed.'}
            format.json { head :no_content }
          end
        end

        def details
            @clients = Client.find_by_id(params[:id])
            @jobs = Client.job
        end
      end

そして、これが現在私のジョブコントローラー用に持っているものです。

class JobsController < ApplicationController

  def index
    @jobs = Job.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml { render :xml => @job }
    end
  end

  def new
    @jobs = Job.new 

    respond_to do |format|
      format.html # index.html.erb
      format.xml { render :xml => @job }
    end
  end

  def create
    @jobs = Job.new(params[:job])
    @cients = Client.find = Client.find(params[:id])

    respond_to do |format|
      if @jobs.save
        format.html { redirect_to(@jobs,
                      :notice => 'Job was successfully created.') }
        format.xml  { render :xml => @jobs,
                      :status => :created, :location => @Job }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @jobs.errors,
                      :status => :unprocessable_entity }
      end
    end
  end
end

私の求人フォームでは、作成されたすべてのクライアントのドロップダウンを追加する次のコードが与えられました。

<%= select("job", "client_id", Client.all.collect {|c| [ c.name, c.id ] }, {:include_blank => 'None'})%> 

ただし、保存を押すと。次のエラーが表示されます。

unknown attribute: client_id

Application Trace | Framework Trace | Full Trace
app/controllers/jobs_controller.rb:22:in `new'
app/controllers/jobs_controller.rb:22:in `create'

これは、ジョブの作成で client_id を見つける方法を定義し、クライアントの作成でそれを指定する必要があるためだと思います。

これは私の最初のRailsアプリですが、方法がよくわかりません。

どんな助けでも大歓迎です。

4

2 に答える 2

1

あなたの協会であなたの仕事とクライアントを複数形にしてください。IE

has_many_and_belongs_to :jobs
has_many_and_belongs_to :clients

また、ActiveRecord :through メソッド (HMABT の代替) を使用したこの多対多関連付けの代替手段を使用しない場合は、job_id と client_id のテーブルである結合テーブルを自分で作成する必要があります。

于 2012-05-09T21:16:28.320 に答える
1

あなたのjobsテーブルには がありませんclient_id。多対多の関係を容易にするために、ジャンクション テーブルを作成する必要があります。それは呼び出さclients_jobsれ、整数client_idとを含む必要がありますjob_id

ここにはもっと多くの間違いがあります。以下は、私が何気なく目にしたものです。

  1. この行:

    @cients = Client.find = Client.find(params[:id])
    

    次のようにする必要があります。

    @cients = Client.find(params[:id])
    
  2. Rails では複数形が重要です。クライアントには多くの「仕事」がありません。多くのジョブあります。モデルはこれを反映する必要があります。

    class Client < ActiveRecord::Base 
      has_and_belongs_to_many :jobs
    end
    
    class Job < ActiveRecord::Base
      has_and_belongs_to_many :clients
    end
    
  3. 外部キーが存在する移行を介してジャンクション テーブルを作成する必要があります。

    $ rails g migration AddClientsJobsTable 
    
  4. indexとではnew、最初に作成@jobs = Job.newし、次に でレンダリングします:xml => @job。繰り返しますが、複数形は重要です。が必要@job = Job.newです。's' を削除して 'J'を大文字にしたcreateことを除いて、 にも同じ問題があります。プログラミングではそれを行うことはできません。大文字と小文字の区別とスペルの両方が重要です。:location => @Job }

  5. Job.find(:all)またはClient.all: 1 つ選択します。find :allとを混ぜないでください.all

  6. @clients = Client.find(params[:id]). クライアントのコレクションではなく、単一の特定のクライアントを見つけています。あなたの変数は と呼ばれるべき@clientです。これはエラーではありませんが、非常に醜いです。
于 2012-05-09T21:18:36.863 に答える