小编Axe*_*ano的帖子

将STI和多态与轨道4结合起来

我正在尝试将STI和多态关联与以下结构相结合的一些问题:

class User < ActiveRecord::Base
end

class Runner < User
  has_many :subscriptions, as: :subscriber, :dependent => :destroy
  has_many :areas, through: :subscriptions
end

class Trainer < Runner
end

class Subscription < ActiveRecord::Base
  belongs_to :subscriber, polymorphic: true
  belongs_to :area
end

class Area
end
Run Code Online (Sandbox Code Playgroud)

当我在控制台中尝试时:

Runner.find(2101).areas

SELECT "areas".* FROM "areas" INNER JOIN "subscriptions" ON "areas"."id" = 
"subscriptions"."area_id" WHERE "subscriptions"."subscriber_id" = $1 AND 
"subscriptions"."subscriber_type" = $2  [["subscriber_id", 2101], ["subscriber_type", 
"User"]]
 => #<ActiveRecord::Associations::CollectionProxy []> 
Run Code Online (Sandbox Code Playgroud)

但是这个跑步者有一个记录:

 #<Subscription id: 3, area_id: 2, subscriber_id: 2101, subscriber_type: "Runner", primary_area: …
Run Code Online (Sandbox Code Playgroud)

polymorphism ruby-on-rails sti

5
推荐指数
1
解决办法
3597
查看次数

Rpsec测试失败了'rspec'但没有'rspec path_of_the_file'

如果我运行此命令"rspec ./spec/requests/api/v1/password_reset_request_spec.rb"此文件中的所有测试都通过.

但是当我运行"rspec"时,我对这个文件中的测试有了一个假设.

  1) /api/v1/password_reset #request when the email match with a runner when there is no request pending create a token for reset the password
 Failure/Error: post("/api/v1/password_reset/request", @params)
 NoMethodError:
   undefined method `reset_password' for RunnerMailer:Class
 # ./app/services/password_manager.rb:35:in `reset_password'
 # ./app/controllers/api/v1/password_reset_controller.rb:31:in `request_new_password'
 # ./spec/requests/api/v1/password_reset_request_spec.rb:108:in `block (5 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

这是调用该方法的行:

 RunnerMailer.reset_password(@identity, @identity.reset_password_token).deliver
Run Code Online (Sandbox Code Playgroud)

这是RunnerMailer类:

class RunnerMailer < ActionMailer::Base
  default from: "no-reply@goodgym.org"

  def reset_password(runner, token)
    @link = "http://url/password_reset/request/" + token
    mail(to: runner.email, subject: "Goodgym -- Reset your password")
  end
end
Run Code Online (Sandbox Code Playgroud)

知道为什么测试在我执行'rspec file_path'时通过而不是在我'rspec'时通过? …

rspec ruby-on-rails actionmailer

3
推荐指数
1
解决办法
1884
查看次数

标签 统计

ruby-on-rails ×2

actionmailer ×1

polymorphism ×1

rspec ×1

sti ×1