Ruby on rails 如何从另一个模型访问模型?
我有两个ROR模型,一个是Ruby on rails 如何从另一个模型访问模型?,ruby-on-rails,ruby,model,Ruby On Rails,Ruby,Model,我有两个ROR模型,一个是Note,另一个是Access。每个访问都有一个备注字段和一个用户字段。在notes控制器的索引操作中,我希望过滤用户拥有的注释(完成)以及用户可访问的注释,我将其命名为@accessible\u notes。 下面的代码为我提供了用户拥有的正确注释,但是我无法让用户访问这些注释 基本上,我需要找到用户参与的所有访问,然后获取相应的注释。我该怎么做 def index @notes = Note.where(user: current_user) @pers
Note
,另一个是Access
。每个访问都有一个备注字段和一个用户字段。在notes控制器的索引操作中,我希望过滤用户拥有的注释(完成)以及用户可访问的注释,我将其命名为@accessible\u notes。
下面的代码为我提供了用户拥有的正确注释,但是我无法让用户访问这些注释
基本上,我需要找到用户参与的所有访问,然后获取相应的注释。我该怎么做
def index
@notes = Note.where(user: current_user)
@personal_access = Access.where("user_id = ?",current_user.id)
@accessible_notes = []
@personal_access.each do |accessible|
tnote = Note.find(accessible.note_id)
@accessible_notes += tnote if tnote
end
end
怎么样
@personal_access.each do |accessible|
@accessible_notes << accessible.note
end
@accessible_notes.flatten!
@personal_access.each do|access|
@可访问的注释类用户:access,:source=>:notes
结束
@可访问的注释=当前用户。可访问的注释
class User < ActiveRecord::Base
has_many :accessible_notes, :through => :accesses, :source => :notes
end
@accessible_notes = current_user.accessible_notes