0

レールの pinterest (オブジェクトのコレクション) でボードのようなものを実装する最良の方法は何ですか。私はそれに来ようとしていますが、それは配列の実装のようです。関連付けのロジックは次のとおりです。ユーザーには多くのコレクションがあり、ユーザーには多くのピンがあり、コレクションはユーザーに属しています。

ユーザークラス

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

ピンクラス

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

end

コレクションクラス

class Collection < ActiveRecord::base
  belongs_to :user
end

コレクションを作成し、このコレクションオブジェクト内でピンを作成またはプッシュし、それらを 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
4

2 に答える 2

1

これには、ネストされた属性を使用する必要があります。

このhttp://currentricity.wordpress.com/2011/09/04/the-definitive-guide-to-accepts_nested_attributes_for-a-model-in-rails-3/を確認してください。

基本的に必要なものは次のとおりです。

# collection model
accepts_nested_attributes_for :pins

# view, see also nested_form in github
f.fields_for :pins
于 2013-06-18T17:42:30.290 に答える