2016-01-07 29 views
0

我有一個item模型,其belongs_to一個product模型。 每個producthas_and_belongs_to_manyspecification型號:has_and_belongs_to_many:如何獲得正確的關係? (JSON輸出)

productsspecifications之間
class Product < ActiveRecord::Base 
    has_many :items 
    has_and_belongs_to_many :specifications 
end 

關係保存內products_specifications JOIN表:

create_table "products_specifications", id: false, force: :cascade do |t| 
    t.integer "product_id" 
    t.integer "specification_id" 
    end 

enter image description here

每個specificationhas_and_belongs_to_manytags

class Specification < ActiveRecord::Base 
    # Product Specifications (e.g. Color, Weight, Size, Brand, Product-Type) 
    has_and_belongs_to_many :products 
    has_and_belongs_to_many :tags # Tags = Spec Detail (e.g. blue, 100 gramm, 5x5x2.5 cm, Apple, Smartphone) 
end 

JOIN表specifications_tags

create_table "specifications_tags", id: false, force: :cascade do |t| 
    t.integer "specification_id" 
    t.integer "tag_id" 
    end 

enter image description here

同時,各taghas_and_belongs_to_manyspecifications

class Tag < ActiveRecord::Base 
    # Product Specifications Tag/Details (e.g. blue, 100 gramm, 5x5x2.5 cm, Apple, Smartphone) 
    has_and_belongs_to_many :specifications  
end 

我嘗試輸出項目的產品與JOIN表相關的規範和JOIN表格相關標籤如下:

def show 
    item = Item.find(params[:id]) 

    render json: item.to_json(include: { 
     product: {include: {specifications: {include: {tags: {}}}}} 
    }) 
    end 

這確實輸出正確的產品型號,但它包含的而不是隻有那些相關的所有規格和數據庫中的所有標籤。目前看起來簡化了這樣的錯誤 JSON輸出(用於item屬於「蘋果iPhone 6智能手機」 product):

"item":{"id":1,"product": 
    {"id":1,"name":"Apple iPhone 6 Smartphone","specifications": 
    [ 
     {"id":1,"name":"Product Type","tags": 
     [ 
      {"id":1,"name":"Smartphone"}, 
      {"id":1,"name":"Smartphone"} 
     ] 
     }, 
     {"id":2,"name":"Brand","tags": 
     [ 
      {"id":2,"name":"Apple"}, 
      {"id":4,"name":"Samsung"} 
     ] 
     }, 
     {"id":3,"name":"Model","tags": 
     [ 
      {"id":3,"name":"iPhone 6"}, 
      {"id":5,"name":"Galaxy A5"} 
     ] 
     } 
    ] 
    } 
} 

目前我只有兩個products數據庫中的:「蘋果iPhone 6智能手機」和「三星Galaxy A5智能手機」。您可以看到它輸出所有產品的規格和標籤,而不是僅相關的has_and_belongs_to_many。該正確 JSON輸出爲item有關「蘋果iPhone 6智能手機」 product看起來像這樣的:

"item":{"id":1,"product": 
    {"id":1,"name":"Apple iPhone 6 Smartphone","specifications": 
    [ 
     {"id":1,"name":"Product Type","tags": 
     [ 
      {"id":1,"name":"Smartphone"} 
     ] 
     }, 
     {"id":2,"name":"Brand","tags": 
     [ 
      {"id":2,"name":"Apple"} 
     ] 
     }, 
     {"id":3,"name":"Model","tags": 
     [ 
      {"id":3,"name":"iPhone 6"} 
     ] 
     } 
    ] 
    } 
} 

編輯:

問題無關與JSON是如何輸出。真正的問題是:

我該如何正確關聯的標籤各自的產品的各自的規範?

+0

?你有沒有嘗試過使用jbuilder? – kjmagic13

+0

Rails 4.2.3,不,我還沒有嘗試過jbuilder,但我剛剛看到gem已經安裝在我的項目中。我將如何與jbuilder做到這一點? –

+0

我已經更新了我的答案,讓我知道如果這有效。 – kjmagic13

回答

1

這是has_and_belongs_to_many關聯的(思考)問題。我切換到HAS_MANY:通過協會,並創建一個額外的模型/表:「product_specs」

create_table "product_specs", force: :cascade do |t| 
    t.integer "product_id" 
    t.integer "specification_id" 
    t.integer "tag_id" 
    t.integer "creator_id" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    end 

現在我可以輕鬆地輸出與JBuilder的權協會(感謝@ kjmagic13指點出來)這樣的:

您正在使用什麼版本的軌道
0

您應該能夠使用JBuilder。沿着線的東西:

# app/views/items/show.json.jbuilder 
json.item do 
    json.extract! @item, :id 
    json.product do 
     json.extract! @item.product, :name 
     json.specifications @item.product.specifications do |spec| 
      json.extract! spec, :name 
      json.tags spec.tags, :id, :name 
     end 
    end 
end 

請務必在您的項目控制器中的show方法來改變item實例變量@item

+0

我想這一點,但它仍然給了我(幾乎)相同的JSON輸出:'{「項目」:{「ID」:1, 「product」:{「name」:「Apple iPhone 6 Smartphone」,「specifications」:[{「name」:「Product Type」,「tags」:[{「id」:1,「name」 },{ 「ID」:1, 「名稱」: 「智能手機」}]},{ 「名稱」: 「商標」, 「標籤」:[{ 「ID」:2 「名稱爲」: 「蘋果」}, {「id」:4,「name」:「Samsung」}]},{「name」:「Model」,「tags」:[{「id」:3,「name」:「iPhone 6」}, 「id」:5,「name」:「Galaxy A5」}]}]}}}'..也許我做了關係錯誤 –

+0

您是否從show controller中刪除了渲染邏輯? – kjmagic13

+0

是的,我outcommented它,從我之前的評論此輸出JSON deffinitely來自show.json.jbuilder文件 –