【问题标题】:How to setup a many-tiered Rails ActiveRecord many-to-many relation?如何设置多层 Rails ActiveRecord 多对多关系?
【发布时间】:2015-02-17 12:40:03
【问题描述】:

已解决请在问题末尾和我下面的回答中阅读更多内容。

接受的问题解决了主要错误。

我自己的回答给出了如何解决细节的描述。


原问题:

我正在尝试建立多对多的关系。我已经阅读了很多关于堆栈交换以及 Rails 的手册和教程的内容。直到今天我还以为我已经明白了该怎么做,但现在我需要沿着整个模型链检索信息,而数据库中什么也没有。

关系是用户成员用户组访问权限函数,成员和访问权限为“通过”模型。

class User < ActiveRecord::Base
  has_many :memberships
  has_many :usergroups, :through => :memberships
  has_many :functions, :through => :usergroups
end

class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :usergroup
end

class Usergroup < ActiveRecord::Base
  has_many :accessrights
  has_many :functions, :through => :accessrights
  has_many :memberships
  has_many :users, :through => :memberships
end

class Accessright < ActiveRecord::Base
  belongs_to :function
  belongs_to :usergroup
end

class Function < ActiveRecord::Base
  has_many :accessrights
  has_many :usergroups, :through => :accessrights
end

我掌握了一个用户对象,我想在列表中获取该用户对象的函数名称。我在帮助模块中尝试这个:

# Loads the functions the current user via her usergroup has access right to (if any).
def load_user_functions
  if @current_user_functions.nil?
    @current_user_functions = []
    if logged_in?
      @current_user.functions.each do |f|
        @current_user_functions << f.name
      end
    end
  end
end

通过阅读手册,我得到的印象是,如果我正确设置模型,我可能会隐式地执行类似 @current_user.functions.each 的操作。

我在网站上放了一个调试器。我在登录后向我显示当前用户,但没有有关功能的信息。登录后它们保持空白:

# Logs in the given user.
def log_in(user)
  session[:user_id] = user.id
  # reset function access rights
  @current_user_functions = nil
  load_user_functions
end

# Returns the current logged-in user (if any).
def current_user
  @current_user ||= User.find_by(id: session[:user_id])
end

# Returns true if the user is logged in, false otherwise.
def logged_in?
  !current_user.nil?
end

这里是表的数据库架构,由我的迁移创建:

create_table "accessrights", id: false, force: true do |t|
  t.integer  "usergroup_id"
  t.integer  "function_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

add_index "accessrights", ["usergroup_id", "function_id"], name: "index_accessrights_on_function_id_and_usergroup_id", unique: true, using: :btree

create_table "functions", force: true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "memberships", id: false, force: true do |t|
  t.integer  "user_id"
  t.integer  "usergroup_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

add_index "memberships", ["user_id", "usergroup_id"], name: "index_memberships_on_user_id_and_usergroup_id", unique: true, using: :btree

create_table "usergroups", force: true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "users", force: true do |t|
  t.string   "username"
  t.string   "email"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "password_digest"
end

这是调试器,我用它检查变量的内容。也许调试器无法处理这样的列表?

<%= debug(@current_user) if Rails.env.development? %>
<%= debug(@current_user_functions) if Rails.env.development? %>

如果它有意义,第二个调试器会显示:

---
...

我的数据库种子的摘录:

@function3 = Function.create!(name:  "ViewCreateEditDeleteUsers")

@usergroup3 = Usergroup.create!(name:  "Admin") # gets right to view, create, edit and delete user groups and users

Accessright.create!(function_id:  @function3.id,
                    usergroup_id: @usergroup3.id)

@user = 
    User.create!(username:  "AOphagen",
             email: "ophagen@test.de",
             password: "testing", password_confirmation: "testing", )

Membership.create!(user_id:  @user.id,
                   usergroup_id: @usergroup3.id)

BroiSatse 提出有用的建议后,我尝试了 rails 控制台(哎呀,应该是我最喜欢的工具),结果是,我已经正确设置了数据库:

User.find_by(username:"AOphagen").functions.first.name
User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."username" = 'AOphagen' LIMIT 1
Function Load (2.1ms)  SELECT "functions".* FROM "functions" INNER JOIN "accessrights" ON "functions"."id" = "accessrights"."function_id" INNER JOIN "usergroups" ON "accessrights"."usergroup_id" = "usergroups"."id" INNER JOIN "memberships" ON "usergroups"."id" = "memberships"."usergroup_id" WHERE "memberships"."user_id" = $1 ORDER BY "functions"."id" ASC LIMIT 1  [["user_id", 1]]
=> "ViewCreateEditDeleteUsers"

已解决主要错误是在数据库设置中。我选择作为解决我的问题的答案解决了这个问题(再次感谢!)。答案简洁、正确且有帮助。

问题的细节需要更多时间来解决,所以我在下面给出了详细的操作方法。

我很想知道为什么我最初的想法(我知道它不像 Ruby)没有奏效 - 以及为什么现在惰性提取可以奏效,拜托!

【问题讨论】:

  • 在您的User 类中没有称为functions 的关系
  • 你可以找到类似的函数 -> functions = @current_user.user_groups.map{| ug |ug.functions}...这是基于您在模型中的关联。
  • @Ansar - 这是典型的 N+1 问题。结果列表也是不可查询的
  • 是的,你是对的@BroiSatse,但我的评论给出了一系列函数,我只是根据当前的关联给出答案

标签: ruby-on-rails ruby ruby-on-rails-4 activerecord has-many-through


【解决方案1】:

对于您要在用户上调用的每个方法,您必须创建一个方法。在这种情况下,您应该像这样使用嵌套的has_many :through

class User < ActiveRecord::Base
  has_many :memberships
  has_many :usergroups, through: :memberships
  has_many :functions, through: :usergroups
end

【讨论】:

  • 啊,你打败了我!我会删除我的答案。 :)
  • 看看我的编辑。现在就是这样。是不是太多了? - 减去 c&p 错字,抱歉。它仍然没有在列表中添加任何内容... ...还有什么遗漏的吗?
  • 现在,我的代码就像你说的那样,BroiSate,但是我页面上的调试器没有显示函数列表中的任何内容。还有什么不妥的地方吗?
  • @AOphagen - 用户实际上有什么功能吗? :)
  • 逐步尝试。首先检查user.memberships,然后是`user.membership.flat_map &:userf groups(不要在您的应用程序中使用此代码- 仅控制台使用:P)等等。找出哪个部分返回不正确的结果并找出原因。顺便说一句 - 使用 FactoryGirl 更容易为测试数据库播种。 :)
【解决方案2】:

微小的平均细节问题是我尝试在log_in(user)函数代码中调用加载函数sn-p(请参阅上面的问题)。

现在我只是在登录后将用户功能列表设置为nil

 # Logs in the given user.
def log_in(user)
  session[:user_id] = user.id
  # reset function access rights
  @current_user_functions = nil
end

就像@current_user 一样,我会等待,直到我第一次需要这些功能,然后加载它们,如果它们仍然是nil

# Returns the currently logged-in users function names as list (if any).
def current_user_functions
  if @current_user_functions.nil?
    @current_user_functions = []
    if logged_in?
      @current_user.functions.each do |f|
        @current_user_functions << f.name
      end
    end
  end
end

(我已经相应地重命名了函数。)

HTML我有一个分支要求某项权利:

<% if hasRightToViewCreateEditDeleteUsers? %>
  <li><%= link_to "Users", '#' %></li>
<% end %>

(那里仍然是一个虚拟链接。)

推测就是这个调用代码加载函数名...

def hasRightToViewCreateEditDeleteUsers?
  current_user_functions.include?("ViewCreateEditDeleteUsers")
end

调试器现在显示函数名称列表:

---
- ViewCreateEditDeleteUsers

即使我在HTML 中的 if 子句还没有显示任何链接,

  1. 我知道我很快就会让它工作
  2. 这无关紧要,因为这首先不是我的问题

万岁!感谢这里每一位乐于助人的人!我会接受BroiSatse 的回答,因为他教了我主要的错误,然后帮我调试了细节。

【讨论】:

    猜你喜欢
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 2018-04-25
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多