2016-03-08 100 views
1

我在通過organization_id將項目模型關聯到組織模型時遇到問題。我開始將項目模型與用戶模型相關聯,但後來我改變主意並決定將每個創建的項目與創建該項目的組織關聯起來。將導軌模型關聯從一個模型更改爲另一個

因此,通過遷移,我插入了一個新列以將organization_id插入到項目模型中。問題是,無論何時我創建一個新項目(以組織身份登錄),organization_id仍爲「無」。我做錯了這個協會不工作?

這是遷移文件:下面

class AddOrganizationIdToProjects < ActiveRecord::Migration 
 
    def change 
 
    \t add_column :projects, :organization_id, :integer, index: true 
 
    end 
 
end

您可以檢查出項目模型和組織模式,具有相應的模式(通過註釋寶石)。

工程模型(用模式)

# == Schema Information 
 
# 
 
# Table name: projects 
 
# 
 
# id    :integer   not null, primary key 
 
# name    :string 
 
# short_description :text 
 
# description  :text 
 
# image_url   :string 
 
# status   :string   default("pending") 
 
# goal    :decimal(8, 2) 
 
# expiration_date :date 
 
# created_at  :datetime   not null 
 
# updated_at  :datetime   not null 
 
# organization_id :integer 
 
# start_date  :date 
 
# 
 

 
class Project < ActiveRecord::Base 
 

 
\t belongs_to :organization 
 

 
end

組織模式(與模式)

# == Schema Information 
 
# 
 
# Table name: organizations 
 
# 
 
# id      :integer   not null, primary key 
 
# email     :string   default(""), not null 
 
# encrypted_password  :string   default(""), not null 
 
# reset_password_token :string 
 
# reset_password_sent_at :datetime 
 
# remember_created_at :datetime 
 
# sign_in_count   :integer   default(0), not null 
 
# current_sign_in_at  :datetime 
 
# last_sign_in_at  :datetime 
 
# current_sign_in_ip  :string 
 
# last_sign_in_ip  :string 
 
# created_at    :datetime   not null 
 
# updated_at    :datetime   not null 
 
# 
 

 
class Organization < ActiveRecord::Base 
 
    # Include default devise modules. Others available are: 
 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
 
    devise :database_authenticatable, :registerable, 
 
     :recoverable, :rememberable, :trackable, :validatable 
 

 
    has_many :projects, dependent: :destroy 
 
end

編輯:添加ING項目控制器創建操作的要求:

\t def create 
 
\t \t @project = Project.new(project_params) 
 

 
    respond_to do |format| 
 
     if @project.save 
 
     format.html { redirect_to @project, notice: 'Project was successfully created.' } 
 
     format.json { render :show, status: :ok, location: @project } 
 
     else 
 
     format.html { render :new } 
 
     format.json { render json: @project.errors, status: :unprocessable_entity } 
 
     end 
 
    end 
 
    end

感謝您的幫助!

+1

你說的organization_ID始終是零。但你如何/在哪裏設置它? – rdupz

+0

你是如何創建項目模型的?請顯示項目控制器創建操作。 – SacWebDeveloper

+0

@SacWebDeveloper根據要求,項目控制器創建操作已啓動。謝謝! – mmgrillo

回答

0

基本上你需要在創建項目時設置organization ID,它不會自動識別。像下面應該做它的東西,這會在你的projects_controller.rb文件中完成:

class ProjectsController < ApplicationsController 
    def create 
     current_organization = Organization.find() #the org id thats logged in 
     Project.create(organization: current_organization) 
     redirect_to #somewhere 
    end 
end 
0

使用單位的關聯方法來創建項目時,它會自動添加的organization_ID。以下代碼假定您需要的組織標識位於current_user上。

def create 
    organization = Organization.find(current_user.organization.id) 
    @project = organization.project.build(project_params) 

    respond_to do |format| 
    if @project.save 
     format.html { redirect_to @project, notice: 'Project was successfully created.' } 
     format.json { render :show, status: :ok, location: @project } 
    else 
     format.html { render :new } 
     format.json { render json: @project.errors, status: :unprocessable_entity } 
    end 
    end 
end 

http://guides.rubyonrails.org/association_basics.html#detailed-association-reference

相關問題