2012-05-09 18 views
0

我的應用程序的基本概述。目前有兩種型號。一個工作模型和一個客戶模型。這兩種型號都有has_and_belongs_to_many關係,因爲我打算允許用戶創建客戶端條目,然後爲其分配一個或多個作業。如何自動爲條目分配一個唯一的ID用於關係?

這是我的兩個模型。

客戶 -

class Client < ActiveRecord::Base 
    has_and_belongs_to_many :job 
end 

工作 -

class Job < ActiveRecord::Base 
    has_and_belongs_to_many :client 
end 

我一直在做一些研究,我認爲在想,這種關係需要一個外鍵功能,從而增加了一個client_id列IM權& a 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 

在我的工作形成我THR以下這增加了下拉與所有的客戶創建代碼中給出。

<%= 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應用程序,雖然我不太清楚如何。

任何幫助將不勝感激。

回答

1

您的jobs表沒有client_id,也不應該。您需要創建一個junction table以促進多對多關係。它應該被稱爲clients_jobs幷包含整數client_idjob_id

還有一個很多這裏更錯了。這裏只是我抓在偶然一瞥的事情:

  1. 這條線:

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

    應該是:

    @cients = Client.find(params[:id]) 
    
  2. 多元化是Rails的重要。客戶沒有很多「工作」。它有很多工作s。你的模型應該反映出這一點:

    class Client < ActiveRecord::Base 
        has_and_belongs_to_many :jobs 
    end 
    
    class Job < ActiveRecord::Base 
        has_and_belongs_to_many :clients 
    end 
    
  3. 你需要通過遷移,這是創建一個junction table哪裏會存在你的外鍵:

    $ rails g migration AddClientsJobsTable 
    
  4. indexnew,你先創建@jobs = Job.new,然後通過:xml => @job進行渲染。再次,多元化是重要的。你需要@job = Job.new。您在create中遇到了同樣的問題,除非您已丟棄's'大寫'J'::location => @Job }您不能在編程中。案例和拼寫都很重要。

  5. Job.find(:all)Client.all:挑一個。請勿混用find :all.all

  6. @clients = Client.find(params[:id])。您正在查找一個單個特定客戶端,而不是客戶端集合。你的變量應該被稱爲@client。這不是錯誤,但它是嚴重醜陋的。
+0

謝謝你的建議。這是我第一次Rails應用程序/嘗試在'後端'編碼,所以原諒任何錯誤:) 唯一的問題。我想我必須從聯結表分配一個客戶IT? 我該怎麼做? – Keva161

+0

您不必以任何方式與交接表交互; Rails會爲你管理它。您只需將一個客戶端分配給一個工作('@job.clients << @ client')或將一個工作分配給客戶端('@client.job = @ job')。 – meagar

+0

當我嘗試創建一個工作,即時獲取'未知屬性:client_id app/controllers/jobs_controller.rb:23:在'new' app/controllers/jobs_controller.rb:23:在'create'' – Keva161

1

複製您的工作和客戶在您的協會。 IE

has_many_and_belongs_to :jobs 
has_many_and_belongs_to :clients 

如果你不使用替代與ActiveRecord的這麼多-to-many關聯:通過方法(替代HMABT)你必須創建連接表自己是JOB_ID的一個表, CLIENT_ID的。

+0

謝謝你的建議! – Keva161

相關問題