【问题标题】:Weird relationship behavior on Rails 3 and update_attributeRails 3 和 update_attribute 上的奇怪关系行为
【发布时间】:2011-07-17 06:38:02
【问题描述】:

我很难找出测试失败的原因:

describe User, "Instance Methods" do
  describe "leave_group!" do
    it "should set group_id to nil" do
      @user = Factory(:user)
      @group_2 = Factory(:group, :owner => @user)

      @user.leave_group!
      @user.reload.group_id.should be_nil # THIS LINE IS FAILING!!!
    end

    it "should assign another owner to group, if owner was user" do
      @user = Factory(:user)
      @group = Factory(:group, :owner => @user)
      1.upto(4) { @group.add_user Factory(:user) }

      @user.leave_group!
      @group.reload.owner.should_not eql(@user)
    end
  end
end

这些是我正在使用的模型:

class User < ActiveRecord::Base
  # Associations
  has_one :own_group, :class_name => "Group", :foreign_key => "owner_id"
  belongs_to :group

  def leave_group!
    current_group_id, current_group_owner = self.group.id, self.group.owner
    self.group_id = nil
    save!
    Group.find(current_group_id).randomize_owner! if current_group_owner == self
  end
end

class Group < ActiveRecord::Base
  # Associations
  has_many :users
  belongs_to :owner, class_name: "User"

  def randomize_owner!
    current_users = self.users
    return false unless current_users.length > 1
    begin
      new_user = current_users.sort_by { rand }.first
    end while self.owner == new_user
    self.owner_id = new_user.id
    save!
  end
end

我在这里做错了吗?我可以改进它吗?更重要的是,为什么单个测试会失败?

这是运行单个测试的日志输出:

SQL (0.2ms)   SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

AREL (0.3ms)  INSERT INTO "users" ("name", "uid", "provider", "email", "image_url", "group_id", "created_at", "updated_at", "timezone", "public_readings", "is_visible_on_leaderboards", "phone_number") VALUES ('John Doe', '314159265', 'facebook', 'john@does.com', NULL, NULL, '2011-07-18 02:02:08.455229', '2011-07-18 02:02:08.455229', NULL, 't', 't', NULL)
Group Load (0.1ms)  SELECT "groups".* FROM "groups" WHERE "groups"."key" = 'SNYEMJ' LIMIT 1
AREL (0.1ms)  INSERT INTO "groups" ("name", "key", "score", "owner_id", "created_at", "updated_at") VALUES ('John''s Group', 'SNYEMJ', 0, 1, '2011-07-18 02:02:08.522442', '2011-07-18 02:02:08.522442')
AREL (0.0ms)  UPDATE "users" SET "group_id" = 1, "updated_at" = '2011-07-18 02:02:08.524342' WHERE "users"."id" = 1
Group Load (0.1ms)  SELECT "groups".* FROM "groups" WHERE "groups"."id" = 1 LIMIT 1
User Load (0.2ms)  SELECT "users".* FROM "users" WHERE ("users".group_id = 1)
User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1

最后 3 行都是选择,注意 rails 甚至没有尝试从用户中删除 group_id。 (有 2 个插入,1 个用于测试用户,1 个用于测试组,1 个更新将 group_id 分配给测试用户)。

【问题讨论】:

  • 请为第一次测试添加logs/test.log的输出,包括SQL查询。
  • 我刚刚添加了它们。请注意,rails 甚至没有尝试进行任何插入或更新,即使它应该进行 2 次更新(1 次用于从用户中删除 group_id,另一次用于将另一个 owner_id 分配给组)。

标签: ruby-on-rails ruby rspec has-many has-one


【解决方案1】:

尝试在测试中在@user.leave_group 之前添加@user.reload 调用。

即使当您从工厂创建@group_2 时,用户记录在数据库中的组已更新,我怀疑@user 对象不是。然后你调用leave_group!@usergroup ID 为零,所以保存不会做任何事情,因为对象没有改变。然后在测试的下一行中重新加载@user,它现在具有先前分配的数据库中的group_id

【讨论】:

  • 非常感谢!我改变了@user.leave_group!到@user.reload.leave_group!它奏效了! :D 我不知道我是怎么错过的。
【解决方案2】:

尝试以下模型类:

class User < ActiveRecord::Base
  # Associations
  has_one :own_group, :class_name => "Group", :foreign_key => "owner_id"
  belongs_to :group

  def leave_group!
    group_randomize_owner
    self.group.clear
  end

  private

  def group_randomize_owner
    if group.owner == self
      group.randomize_owner!
    end
  end
end

【讨论】:

  • 这不起作用:self.group.clear 会抛出一个错误,因为它会尝试调用 Group#clear 这不是一个真正的方法。然而,这将适用于这样的组:@group.users.clear 但这不是我需要解决的情况。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多