尝试实现 has_and_belongs_to_many 关系 一个文档有很多组 一组有很多文档
创建了一个连接表调用documents_group
模型
class Group < ActiveRecord::Base
has_many :users
has_and_belongs_to_many :documents
end
模型 2
class Documents < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :groups
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
before_post_process :resize_images
def image?
avatar_content_type =~ %r{^(image|(x-)?application)/(bmp|gif|jpeg|jpg|pjpeg|png|x-png)$}
end
private
def resize_images
return false unless image?
end
end
控制器创建 def create @document = Documents.all
@document = Documents.create( params[:document] )
@document.user_id = current_user.id
@document.save
redirect_to root_path
end
迁移是
def self.up
create_table :documents_groups ,:id => false do |t|
t.integer :documents_id
t.integer :group_id
end
end
现在我想访问所有文档对应一个组怎么办