2016-07-16 3 views
0

この場合、カテゴリコントローラを作成する必要があるのでしょうか。現在、#indexアクションと#showアクションを持つアイテムコントローラがあります。私はitems#indexアクションで特定のカテゴリのフィルタを追加することを考えていましたが、カテゴリコントローラからはるかに簡単です。私は、「特定のカテゴリのために使用可能なアイテムのリスト」を返すように構築していAPIを必要としRails 4、has_many throughは、特定のカテゴリに対して利用可能な項目のリストを返します。

class Category < ActiveRecord::Base 
    has_many :categorizations 
    has_many :items, :through => :categorizations 
end 

class Item < ActiveRecord::Base 
    has_many :categorizations 
    has_many :categories, :through => :categorizations 
end 

class Categorization < ActiveRecord::Base 
    belongs_to :item 
    belongs_to :category 
end 

:ここでは効果的アイテムとカテゴリとの関係を通じてhas_manyのを設定している私の団体です。それはそうのように(私はまだありません)カテゴリコントローラからこれに非常に簡単です:それは、これを行うために、より良く、より安らか

category.items

ですか?それとも私は現在、このようになりますこれは、私の項目番号のindexアクションでフィルタを作成する必要があります。

もちろん
# Returns full list of items 
def index 
    @items = Item.all 
    render json: @items 
end 

、あなたがベストプラクティスと/インラインより効率的に任意のアイデアを持っている場合 - 私に教えてください!

ありがとうございます!

EDIT - 一つの解決策:

categories/:id/available_items

class CategoriesController < ApplicationController 
    def available_items 
     @available_items = Category.find(params[:id]).items.available 
     render json: @available_items 
    end 
end 
:私は、次の相対パスを使用して、特定のカテゴリに利用可能なアイテムにアクセスできるように、カテゴリのコントローラを追加することにしました

基準は、特定のカテゴリに関連付けられ、ステータスが「利用可能」であるすべてのアイテムを返すことでした。

EDIT:

私はItem.where(category: 1)がbyebugコンソール出力は、以下を参照してくださいカテゴリ1に分類されているすべてのアイテムを返していないことを発見しています:あなたの提案されたソリューションの

1: class ItemsController < ApplicationController 
    2: # Returns full list of items 
    3: def index 
    4:  @items = Item.all 
    5:  byebug 
=> 6: end 
    7: 

(byebug) Item.where(category: 1) 
    Item Load (0.6ms) SELECT "items".* FROM "items" WHERE "items"."category" = 1 
#<Item::ActiveRecord_Relation:0x007fb5d1a37f08> 
(byebug) Category.find(1).items 
    Category Load (0.7ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = $1 LIMIT 1 [["id", 1]] 
    Item Load (1.7ms) SELECT "items".* FROM "items" INNER JOIN "categorizations" ON "items"."id" = "categorizations"."item_id" WHERE "categorizations"."category_id" = $1 [["category_id", 1]] 
#<ActiveRecord::Associations::CollectionProxy [#<Item id: 1, title: "Gorgeous Cotton Pants", description: "Dolor dicta suscipit aut cupiditate quia officiis ...", price: 73960, status: 0, published_date: "2016-07-14 05:35:49", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 1>, #<Item id: 5, title: "Sleek Marble Shoes", description: "Qui mollitia corporis qui placeat. Reiciendis ea s...", price: 35146, status: 0, published_date: "2016-07-14 05:45:02", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 1>, #<Item id: 7, title: "Rustic Concrete Lamp", description: "Sit odio non exercitationem. Atque non sapiente vo...", price: 82016, status: 2, published_date: "2016-07-13 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 1>, #<Item id: 10, title: "Awesome Wooden Table", description: "Possimus consequatur nulla. Quidem molestiae volup...", price: 59519, status: 2, published_date: "2016-07-09 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 1>, #<Item id: 12, title: "Lightweight Concrete Bag", description: "Amet ullam assumenda eligendi consectetur quae. Bl...", price: 72081, status: 2, published_date: "2016-07-16 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 2>, #<Item id: 13, title: "Mediocre Plastic Computer", description: "Excepturi modi est non qui iusto. Molestiae offici...", price: 94357, status: 2, published_date: "2016-07-15 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 2>, #<Item id: 15, title: "Incredible Plastic Bag", description: "Vel voluptas ducimus soluta atque voluptatem eum. ...", price: 15661, status: 2, published_date: "2016-07-14 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 2>, #<Item id: 16, title: "Lightweight Iron Watch", description: "Id sequi rerum dolor sit sunt nemo laborum. Omnis ...", price: 65306, status: 4, published_date: "2016-07-11 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 1>, #<Item id: 17, title: "Rustic Linen Chair", description: "Explicabo qui ad nihil. Voluptatem placeat autem. ...", price: 39752, status: 4, published_date: "2016-07-04 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 1>, #<Item id: 18, title: "Mediocre Copper Car", description: "Minus qui ut est non vero saepe. Qui sed quos et v...", price: 87765, status: 4, published_date: "2016-07-05 00:00:00", created_at: "2016-07-17 05:15:07", updated_at: "2016-07-17 05:15:07", seller_id: 1>]> 

答えて

1

の両方があります有効な、しかしのは、いくつかのコードを見てみましょう:

オプション1 - Railsのデフォルト

resources :categories do 
    resources :items 
end 

# /categories/42/items/7 

RailsはこれをItemsControllerにルーティングします。これはcategory_idが渡された何らかの処理を担当します。アプリケーションのアーキテクチャ/ロジックが実際にでない場合は、を別の方法で実行するように要求します。このアプローチで。

オプション2 - コントローラを示すの概念が自然は、そのアイテムを表示するかどう「ショーカテゴリー」

resources :categories, :items 

class CategoriesController < ApplicationController 
    def index 
    @categories = Category.all 
    end 

    def show 
    @category = Category.includes(:items).find params[:id] 
    end 
end 

このアプローチは素晴らしいです。これは、多対多の関係の典型的な性質を考えると、あなたのケースでは適切ではないかもしれませんが、本当にコンテキストに依存します。

オプション3 - 三のTidyのコントローラ

あなたのitems#showitems#index方法が過度に条件付き取得し始めていることが判明した場合、私はコントローラを追加し、あなたのルーティングとを変更することになります。

class CategorizedItemsController < ApplicationController 
    # ... 
end 

resources :categories 
resources :items 
scope '/categories/:category_id/' do 
    resources :items, controller: :categorized_items 
end 

これはあなたのプロジェクトに参加している他の開発者にとってはあまり明らかではなく、あなたのルートについての推論を少し難しくし始めます。ただし、既存のコントローラー階層がRESTfulルーティングの範囲内でアクションを表すことができない場合は、採用することを躊躇しないでください。

乾杯!

+0

ありがとうcorey - 私が今行っているソリューションを表示するためのアップデートを追加しています。多分あなたはあなたの考えを知らせることができます。 – matthewalexander

+0

オプション1から私を抱きしめていたことの1つは、category_id = xのすべてのアイテムを返す方法がわかりませんでした。任意のヒント? – matthewalexander

+0

@matthewalexanderあなたはすでにそれをしています: 'Category.find(x).items'。 'Item.where(category:x)'を実行することもできます。これは、 'Item'に他のスコープを使用できるようにします(例えば' .available')。 – coreyward

関連する問題