私は基本的にコレクションモデルとポストモデルを持っています。コレクションには多くのポストがあり、ポストは多くのコレクションに属しています。だから私は時折コレクションにを追加というポストを複製するために、<<
を使用して@collection.posts
配列に投稿をプッシュします。今、彼らはその配列にプッシュされた時点で@collection.posts
の投稿を注文する方法はありますか?はいの場合、どうですか?アイテムをその配列に追加するまでに配列に配置しますか?
関連するすべてのモデル:
user.rb
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
has_many :collections, dependent: :destroy
end
post.rb
class Post < ActiveRecord::Base
belongs_to :user
has_many :collectables
has_many :collections, through: :collectables
end
collection.rb
私は協会の定義に注文スコープを追加するとうまくいくと思いますclass Collection < ActiveRecord::Base
belongs_to :user
has_many :collectables
has_many :posts, through: :collectables
end
collectable.rb
class Collectable < ActiveRecord::Base
belongs_to :post
belongs_to :collection
end
あなたのテーブルに参加しない方法は次のようになり? 'collection_id'と' post_id'の2つのカラムしかありませんか?あるいは 'created_at'のような他の列がありますか? – spickermann
@spickermann私は実際にここでそれについてより詳細な質問をしました - http://stackoverflow.com/questions/39188708/controller-action-for-adding-a-post-to-a-collection-model-railsそれが長すぎたので誰もそれに反応しなかったと考えました。だから私は、より現実的な質問をすることにしました。それにもかかわらず、私はこの問題をすべての関連モデルコードで更新しました – Arif
あなたの 'collectables'データベーステーブルにはどの列がありますか? – spickermann