《工厂女孩》中的依赖属性

发布于 2024-10-07 04:54:52 字数 1258 浏览 5 评论 0原文

经过几个小时的谷歌搜索和测试后,我似乎应该能够找到这个问题的明显答案。

我希望能够设置 caredate.user_id => caredate工厂内的provider.user_id。

测试错误:

ActiveRecord::RecordInvalid: 验证失败:用户必须相同 作为提供商用户

我有一个 ActiveRecord 验证,该验证在通过浏览器测试时有效:

class Caredate < ActiveRecord::Base //works fine when testing via browser
  belongs_to :user
  belongs_to :provider

  validates_presence_of :user_id
  validates_presence_of :provider_id
  validate :user_must_be_same_as_provider_user

  def user_must_be_same_as_provider_user
    errors.add(:user_id, "must be same as provider user") unless self.user_id == self.provider.user_id
  end

end

//factories.rb
Factory.define :user do |f| 
  f.password "test1234"
  f.sequence(:email) { |n| "foo#{n}@example.com" }
end

Factory.define :caredate do |f| 
  f.association :provider
  **f.user_id { Provider.find_by_id(provider_id).user_id }  //FAILS HERE**
end

Factory.define :provider do |f| 
  f.association :user
end

如果之前已经回答过这个问题,我深表歉意;我尝试了几种不同的选择,但无法使其发挥作用。

更新:这通过了验证,所以我越来越接近了。我可以用随机数进行破解。

Factory.define :caredate do |f| 
  f.association :user, :id => 779
  f.association :provider, :user_id => 779
end

Seems like I should have been able to find an obvious answer to this problem after a few hours of Googling and testing.

I want to be able to set caredate.user_id => provider.user_id within the caredate factory.

Test Error:

ActiveRecord::RecordInvalid:
Validation failed: User must be same
as provider user

I have an ActiveRecord validation which works when tested via the browser:

class Caredate < ActiveRecord::Base //works fine when testing via browser
  belongs_to :user
  belongs_to :provider

  validates_presence_of :user_id
  validates_presence_of :provider_id
  validate :user_must_be_same_as_provider_user

  def user_must_be_same_as_provider_user
    errors.add(:user_id, "must be same as provider user") unless self.user_id == self.provider.user_id
  end

end

//factories.rb
Factory.define :user do |f| 
  f.password "test1234"
  f.sequence(:email) { |n| "foo#{n}@example.com" }
end

Factory.define :caredate do |f| 
  f.association :provider
  **f.user_id { Provider.find_by_id(provider_id).user_id }  //FAILS HERE**
end

Factory.define :provider do |f| 
  f.association :user
end

My apologies if this has been answered previously; I tried several different options and couldn't get it to work.

Update: This passes validation, so I'm getting closer. I could hack with a random number.

Factory.define :caredate do |f| 
  f.association :user, :id => 779
  f.association :provider, :user_id => 779
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝眼泪 2024-10-14 04:54:52
Factory.define :caredate do |f|
  provider = Factory.create(:provider)
  f.provider provider
  f.user provider.user
end
Factory.define :caredate do |f|
  provider = Factory.create(:provider)
  f.provider provider
  f.user provider.user
end
此岸叶落 2024-10-14 04:54:52

尝试在 after_createafter_build 中设置 user_id:

Factory.define :caredate do |f|
  f.after_create { |caredate| caredate.user_id = caredate.provider.user_id }
end

Try setting the user_id in after_create or after_build:

Factory.define :caredate do |f|
  f.after_create { |caredate| caredate.user_id = caredate.provider.user_id }
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文