2017-01-30 19 views
0

我們中有幾個人正在web api上工作,其中一位版主想要有一個擁有許多活動的社區,而且這些活動有很多活動門票。通過深嵌套在軌道上發佈對象

以下是routes.rb文件的外觀。

Rails.application.routes.draw do 
    resources :communities, :defaults => { :format => 'json' } do 
    resources :events do 
     resources :tickets 
    end 
    end 
end 

我已經得到了我的RSpec請求測試門票的工作,除了POST。

這裏的門票控制器

class TicketsController < ApplicationController 

    before_action :set_community, only: [:create] 
    before_action :set_event,  only: [:index, :create] 
    before_action :set_ticket,  only: [:show, :update, :destroy] 

    def index 
    @tickets = @event.tickets 
    render json: @tickets 
    end 

    def create 
    @ticket = @event.tickets.build(ticket_params) 
    if @ticket.save 
     render json: @ticket, status: :created, location: [@community, @event, @ticket] 
    else 
     render json: @ticket.errors, status: :unprocessable_entity 
    end 
    end 


    def show 
    render json: @ticket 
    end 

    def update 
    if @ticket.update(ticket_params) 
     render json: @ticket 
    else 
     render json: @ticket.errors, status: :unprocessable_entity 
    end 
    end 

    def destroy 
    if @ticket.destroy 
     render json: @ticket 
    else 
     render json: @ticket.errors, status: :unprocessable_entity 
    end 
    end 

    private 
    def ticket_params 
    params.require(:ticket).permit(:name, :cost, :quantity, 
           :sale_starts_at, :sale_ends_at) 
    end 

    def set_ticket 
    @ticket = Ticket.find(params[:id]) 
    end 

    def set_event 
    @event = Event.find(params[:event_id]) 
    end 

    def set_community 
    @community = Community.find(params[:community_id]) 
    end 

end 

在創建功能我寫@event.tickets.build(ticket_params), 但我得到驗證錯誤,它說我需要的社區。

我想@community.event.tickets.build(ticket_params)的工作,但我從@community得到一個無方法錯誤。

這也許不是最好的做法,深入巢這樣的,但我不知道它如何工作。

下面是模型

class Community < ApplicationRecord 
    has_many :events 
    has_many :tickets, through: :event 

    validates :name, presence: true 
    validates :description, presence: true 
end 

class Event < ApplicationRecord 
    belongs_to :community 
    has_many :tickets 

    validates :name, presence: true 
    validates :description, presence: true 
    validates :event_starts_at, presence:true 
    validates :event_ends_at, presence:true 

end 

class Ticket < ApplicationRecord 
    belongs_to :event 
    belongs_to :community 

    validates :name, presence: true 
    validates :cost, presence: true, 
        numericality: { greater_than_or_equal_to: 0 } 
    validates :quantity, presence: true, 
         numericality: { greater_than_or_equal_to: 1 } 
    validates :sale_starts_at, presence: true 
    validates :sale_ends_at, presence: true 
end 

我試圖在門票模型寫belongs_to :community, through: :event,但由於某種原因,我得到這個錯誤: ArgumentError: Unknown key: :through. Valid keys are: :class_name, :anonymous_class, :foreign_key, :validate, :autosave, :foreign_type,等等

任何幫助將是巨大的,提前致謝。

回答

1

的問題是,你應該設置票和社區之間的關係要經過events,而不是重複的外鍵。

class Ticket < ApplicationRecord 
    belongs_to :event 
    has_one :community, through: :event 
end 

這將導致事件作爲一個連接表這是一個很好的事情,因爲ActiveRecord的只保留一個外鍵關係的軌道運行,當你創建一個關聯的資源。

has_onebelongs_to之間的區別是belongs_to地方上模型外鍵列,而has_one其放在其他型號。這就是爲什麼一個belongs_to through:關係是不可能的。

+0

非常感謝你,我認爲這讓我走上了正軌。我還沒有完全想到,但我現在可以發佈!因爲票據是在社區「之下」,它並沒有讓我想到'has_one'會起作用,但它確實經歷了。再次感謝! – gazayas

+1

如果此答案對您有幫助,您應該按右側的複選標記來接受答案。 – max