안녕하세요 수집 모델로 함께 합쳐진 게시물 모델과 컬렉션 모델이 있습니다. 사용자가 소식을 올리면 소식을 '음악'과 같은 소식 모음에 추가합니다. 그러나 모든 사용자의 컬렉션을 나열 할 때 각 게시물마다 여러 개의 '음악'항목이 있습니다. @collections = @ user.posts.map (& : 모음) .flatten으로 컬렉션을 가져옵니다. 끝에 .uniq를 추가하면 중복이 없습니다 (@collections = @ user.posts.map (& : collections) .flatten.uniq)하지만 누군가가 왜이 작업을해야하는지 설명 할 수 있습니까 ??? 고마워.동일한 컬렉션 이름을 가진 모든 게시물에 대해 중복 된 이유는 무엇입니까
UsersController
def show
@user = User.find(params[:id]) rescue nil
@posts = @user.posts.paginate(:per_page => "10",:page => params[:page])
@title = @user.name
@collections = @user.posts.map(&:collections).flatten
end
보기/사용자/show.html.erb
<h1>Collections</h1>
<% @collections.each do |collection| %>
<%= link_to collection.name, user_collection_posts_path(@user, link_name(collection)) %><br />
<% end %>
컬렉션 모델
class Collection < ActiveRecord::Base
mount_uploader :image, CollectionUploader
attr_accessible :name, :image, :user_id
has_many :collectionships
has_many :users, :through => :posts
has_many :posts, :through => :collectionships
end
collectionship 모델
class Collectionship < ActiveRecord::Base
belongs_to :post
belongs_to :collection
has_one :user, :through => :post
attr_accessible :post_id, :collection_id
end
,617,
후 모델 당신은 그것을 일으키는 라인을 가지고
has_many :posts, :dependent => :destroy
has_many :collections, :through => :posts