English 中文(简体)
铁路局:通过联系
原标题:Ruby on Rails - Accessing data in :has_many => :through association

I m试图控制这种铁路协会

我有三个模式:

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 

我有预选方法,为违约用户小组增加一个新的用户。

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

我试图写一个检验标准,以确保正确进行。

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?

如果我使用用户,那么测试便可进行。 我的印象是,我应该能够通过小组接触这些团体?

我如何检验用户是否属于称为“积极用户”的违约小组?

任何建议都将受到高度赞赏。

增 编

最佳回答

你可能会克服违约小组的重复。 这里是我来的。 用户和团体在我的例子中只有一个名字属性,以保持其简单。

# 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

你们只需要一个回响,? 现在用于测试:

# 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

这项测试不仅检查了用户的~>m> 类别,而且检查了您重新期望的准确类别,这是一个更严格的检查。

问题回答

暂无回答




相关问题
Selenium not working with Firefox 3.x on linux

I am using selenium-server , selenium rc for UI testing in my application . My dev box is Windows with FireFox 3.5 and every thing is running fine and cool. But when i try to run selenium tests on my ...

Best browser for testing under Safari Mobile on Linux?

I have an iPhone web app I m producing on a Linux machine. What s the best browser I can use to most closely mimic the feature-limited version of Safari present on the iPhone? (It s a "slimmed down" ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Is there any error checking web app cralwers out there?

Wondering if there was some sort of crawler we could use to test and re-test everything when changes are made to the web app so we know some new change didn t error out any existing pages. Or maybe a ...