简体   繁体   English

Ruby on Rails-通过关联访问:has_many =>中的数据

[英]Ruby on Rails - Accessing data in :has_many => :through association

I'm trying to get to grips with this type of association in Rails 我正在尝试掌握Rails中的这种关联

I have the following three models: 我有以下三种模型:

User

has_many: 'memberships'
has_many: groups, :through => 'memberships'

Membership

belongs_to: 'user'
belongs_to 'group'

Group

has_many: 'memberships'
has_many: 'users', :through => 'memberships'

I have a before_create method which adds a new user to a default user group. 我有一个before_create方法,它将新用户添加到默认用户组。

user.rb

  attr_accessor :group_ids

  before_create :add_user_to_default_group
  after_save :update_groups

  def add_user_to_default_group
      self.group_ids = [1]
  end



 #after_save callback to handle group_ids
  def update_groups
    unless group_ids.nil?
      self.memberships.each do |m|
        m.destroy unless group_ids.include?(m.group_id.to_s)
        group_ids.delete(m.group_id.to_s)
      end 
      group_ids.each do |g|
        self.memberships.create(:group_id => g) unless g.blank?
      end
      reload
      self.group_ids = nil
    end
  end

I'm trying to write a test to ensure this is happening correctly. 我正在尝试编写测试以确保正确进行。

user = User.new
user.username = 'testuser'
user.email = 'testuser@testdomain.co.uk'
user.password = 'testpass'
user.password_confirmation = 'testpass'
assert user.save
assert user.errors.empty?
assert !user.memberships.empty?

The test passes if I use user.memberships.empty, but not if I use user.groups.empty?. 如果使用user.memberships.empty,则测试通过,但如果使用user.groups.empty ?,则测试未通过。 I was under the impression I should be able to access the groups through .groups? 我的印象是我应该能够通过.groups访问网上论坛?

How would I test that the user belongs to a default group called 'Active Users'? 如何测试用户是否属于默认组“ Active Users”?

Any advice would be greatly appreciated. 任何建议将不胜感激。

Thanks 谢谢

You might be overcomplicating the default group. 您可能使默认组过于复杂。 Here's what I came up with. 这是我想出的。 Users and Groups only have a name attribute in my examples, to keep it simple. 为了简单起见,用户和组在我的示例中仅具有名称属性。

# app/models/user.rb
class User < ActiveRecord::Base
  has_many :memberships
  has_many :groups, :through => :memberships

  before_create :add_default_group

  protected

  def add_default_group
    self.groups << Group.find_by_name('Active Users')
  end
end

# app/models/group.rb 
class Group < ActiveRecord::Base
  has_many :memberships
  has_many :users, :through => :memberships
end

# app/models/membership.rb 
class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

You only need one callback, before_create. And now for the test: 您只需要一个回调, before_create. And now for the test: before_create. And now for the test:

# test/unit/user_test.rb 
require 'test_helper'

class UserTest < ActiveSupport::TestCase
  def test_user_gets_default_group
    group = Group.create(:name => 'Active Users')
    user = User.new(:name => 'Test')

    assert user.save
    assert_equal [group], user.groups
  end
end

This test is not just checking that the user has some groups, but the exact group you're expecting, which is a tighter check. 该测试不仅检查用户是否有一些组,还检查您期望的确切组,这是一个更严格的检查。

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

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