2013-06-18 53 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