2013-06-18 50 views
0

什麼是最好的方式來實現像鐵軌pinterest(對象的集合)董事會的東西。我試圖與它一起來,它似乎更像是一個數組實現。 這裏是我的關聯邏輯:用戶有很多集合,用戶有很多針,集合屬於用戶。協會的困境

User類

class User < ActiveRecord::Base 
    has_many :pins, through: :collections 
    has_many :collections 
end 

銷類

class Pin < ActiveRecord::Base 
belongs_to :user 
has_many :collections 

end 

Collections類

class Collection < ActiveRecord::base 
    belongs_to :user 
end 

所以,現在,這裏是我的困惑,如何實現一個控制器,讓我來創建一個集合並在該集合對象內部創建或推入引腳並將其另存爲另一個t對象他current_user。希望我正在感

這裏的控制器

class CollectionsController < ApplicationController 
    def create 
    @collection = current_user.collections.new(params[:collection]) 
    #this where i'm confused , if it an array , how to implement it , to push or create a pin object inside ? 
    end 

end 

回答

0

您正在尋找的has_many_through關聯。請參閱Rails指南中的第2.4節:http://guides.rubyonrails.org/association_basics.html

class User < ActiveRecord::Base 
    has_many :collections 
end 

class Pin < ActiveRecord::Base 
    has_many :collections, through: :pinnings 
end 

class Pinning < ActiveRecord::Base 
    belongs_to :pin 
    belongs_to :collection 
end 

class Collection < ActiveRecord::base 
    belongs_to :user 
    has_many :pins, through: :pinnings 
end