我有很多具有高级关系的资源(habtm/hm/hmt 等),你可以想象的一切,但现在是时候为这个 API 编写一个漂亮的路由了。问题是,我无法找到关于嵌套资源 + 高级关系的最佳实践来做我的路由,这是我想要做的:
这是我的模型与相关关系
# app/models/candidate.rb
class Candidate < ApplicationRecord
include Sociable, Locatable
belongs_to :user
has_many :sourcing_accounts
has_many :accounts, through: :sourcing_accounts
has_many :users, through: :sourcing_accounts
end
# app/models/sourcing_account.rb
class SourcingAccount < ApplicationRecord
belongs_to :account
belongs_to :candidate
belongs_to :user
end
# app/models/user.rb
class User < ApplicationRecord
include Sociable
has_many :candidates
has_many :campaigns
has_many :sourcing_account
end
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我愿意允许创建之间的关系Candidate和User通过创建SourcingAccount。
resources :candidates do
resources :accounts
resources :users, only: [:index] do
post :remove
post :add
end
end …Run Code Online (Sandbox Code Playgroud) 我正在浏览一些代码并发现了这个:
stem = ""
answer = ""
return if stem.nil? || answer.nil? || \
stem.question == answer.question
Run Code Online (Sandbox Code Playgroud)
有什么\用?我知道\用于字符串,但我以前从未见过用例.这是语法错误还是一些高级ruby语法?我错过了什么吗?