0
我需要設置一個API終點,它將返回current_user爲賣家的所有項目。我盡我所能去建立REST風格的路線所以對我有這樣的:Rails,返回current_user爲賣方的所有Items的API端點?
的routes.rb
Rails.application.routes.draw do
resources :users
resources :items
get "users/:id/items" => "users#items"
...
users_controller.rb
這將返回所有項目,其中seller_id列等於PARAMS [:ID]。最後,我希望爲items表添加buyer_id列,並且能夠過濾出來 - 所以我需要區分買方和賣方。很明顯,我在這裏沒有這樣做。
class UsersController < ApplicationController
def seller_items
User.find(params[:id]).items
end
end
item.rb的
class Item < ActiveRecord::Base
belongs_to :seller, :class_name => 'User', :foreign_key => 'seller_id'
enum status: {available: 0, pending: 1, sold: 2, expired: 3, banned: 4 }
end
user.rb
class User < ActiveRecord::Base
has_many :items, :class_name => 'Item', :foreign_key => 'seller_id'
end
schema.rb
ActiveRecord::Schema.define(version: 20160715043217) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "categories", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "items", force: :cascade do |t|
t.string "title"
t.text "description"
t.integer "price", limit: 8
t.integer "status"
t.datetime "published_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "seller_id"
end
add_index "items", ["seller_id"], name: "index_items_on_seller_id", using: :btree
create_table "users", force: :cascade do |t|
t.string "name"
t.decimal "latitude", precision: 9, scale: 6
t.decimal "longtitude", precision: 9, scale: 6
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_foreign_key "items", "users", column: "seller_id"
end
你想_buyer_,並且向我們展示_seller_不工作。我現在很困惑:)問題是什麼? –
那麼,有沒有真正的問題? –
我覺得他很疑惑如何根據他所做的銷售物品添加購買物品 –