繁体   English   中英

Rails多对多设置

[英]Rails many-to-many setup

我正在寻求实现这样的状态系统:

# Group examples: user, post, comment
class StatusGroup < ActiveRecord::Base
  has_many :status_group_options
  has_many :status_options, -> { uniq }, through: :status_group_options
end

# Option examples: active, open, closed, banned, completed, deleted
class StatusOption < ActiveRecord::Base
  has_many :status_group_options
  has_many :status_groups, -> { uniq }, through: :status_group_options
end

# This would be to handle which groups have access to which options.
# For example, the user group wouldn't have access to the
# [open, closed, completed] options but would have access
# to the [active, banned, completed].
# StatusGroupOption also has a column labelled as admin to determine
# which statuses only admins can see.
class StatusGroupOption < ActiveRecord::Base
  belongs_to :status_group
  belongs_to :status_option
  has_many :users
end

然后, userpostcomment表将具有一个指向status_group_option_id的链接。

我的问题是,我认为设置不正确。 搜索打开选项或禁止选项中的所有用户似乎过于困难。 此外,仅在“用户”组的“删除”选项中搜索(使用范围)这些表非常困难。

这是正确的方法吗? 我是否使事情复杂化? 我正在遵循指南。

由于您使用的是Rails 4,因此我建议您调查Enum 。这有望简化许多混乱。 除了创建单独的模型外,您可以做的只是在每个模型中实现enum的使用,这样每个模型都有自己的一组状态。

像这样:

class User < ActiveRecord::Base
    enum :status => [:active, :banned, :completed]
end

class Post < ActiveRecord::Base
    enum :status => [:open, :closed, :deleted]
end

class Comment < ActiveRecord::Base
    enum :status => [:open, :closed, :deleted, :flagged] # <= I threw "flagged" in there to show some variance
end

基本上, enum引用表中基于整数的列(在这种情况下,我用:status表示数据库中的status列)。 此处的数组按其索引(0,1,2)枚举其中的不同元素( [:active, :banned, :completed]等)。

如果要保存状态为active的用户,则可以在控制器中执行以下操作:

class UsersController < ApplicationController

   def update
       @user = User.find(params[:id])
       @user.active! #=> updates the user's status as 0, since it was the first element in our enum array.


       # here's another example, say if you wanted to update the user's status to "banned"
       @user.banned! #=> updates the user's status as 2 in the database, since it was the third element in our array
   end
end

因此,在数据库中您将看到整数值,但假设您要在视图中显示状态的实际文本:

show.html.erb

<!-- Instead of displaying the integer that's in your status column, it will display the text automagically of whatever status is set on that record -->
<%= @user.status %>  

<!-- You can even do super convenient conditionals! Thanks, Rails! --> 
<% if @user.active? %>
    This user is active!
<% elsif @user.banned? %>
    This user was banned!
<% end %>

简而言之,我想说最好在每个模型中创建“状态选项”,并使用Rails 4中提供的enum功能定义“状态选项”。

有关更多信息,请访问: http : //edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

您很可能必须将这些“状态”列添加到需要它的每个表中:

rails g migration add_status_column_to_users status:integer
rails g migration add_status_column_to_posts status:integer
rails g migration add_status_column_to_comments status:integer

rake db:migrate

希望这可以帮助! 当我想要具有需要状态(活动,非活动)和角色(例如admin,normal_user等)的模型时,通常会使用enum来构建Rails应用程序。 让我知道您是否需要更好的说明。 我是来帮忙的!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM