我是ActiveRecord新查询界面的新手,所以我还在搞清楚.
我希望有人可以解释scope在ActiveRecord模型中使用a和使用类方法之间的区别(即self.some_method)
从我可以收集的内容来看,范围总是希望返回一个关系,而一个类方法不一定必须.这是真的?
例如,我认为做以下事情是有意义的:
class Person
scope :grouped_counts, group(:name).count
end
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我收到此错误:
ArgumentError: Unknown key(s): communicating, failed, matched, unmatched
from /Users/bradrobertson/.rvm/gems/ruby-1.9.2-p180@influitive/gems/activesupport-3.0.5/lib/active_support/core_ext/hash/keys.rb:43:in `assert_valid_keys'
from /Users/bradrobertson/.rvm/gems/ruby-1.9.2-p180@influitive/gems/activerecord-3.0.5/lib/active_record/relation/spawn_methods.rb:110:in `apply_finder_options'
from /Users/bradrobertson/.rvm/gems/ruby-1.9.2-p180@influitive/gems/activerecord-3.0.5/lib/active_record/named_scope.rb:110:in `block in scope'
from (irb):48
from /Users/bradrobertson/.rvm/gems/ruby-1.9.2-p180@influitive/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in `start'
from /Users/bradrobertson/.rvm/gems/ruby-1.9.2-p180@influitive/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in `start'
from /Users/bradrobertson/.rvm/gems/ruby-1.9.2-p180@influitive/gems/railties-3.0.5/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
r
Run Code Online (Sandbox Code Playgroud)
但它确实可以作为一种类方法
def self.grouped_counts
group(:name).count
end
Run Code Online (Sandbox Code Playgroud)
我很想知道人们关于何时使用范围以及何时使用类方法的想法.我是否正确假设范围必须始终返回关系,但是类方法可以返回它想要的任何内容?
Rails 4允许您has_many像这样关系范围:
class Customer < ActiveRecord::Base
has_many :orders, -> { where processed: true }
end
Run Code Online (Sandbox Code Playgroud)
因此,只要您这样做,customer.orders您只能获得已处理的订单.
但是如果我需要使where条件动态呢?如何将参数传递给范围lambda?
例如,我只希望显示客户当前在多租户环境中登录的帐户的订单.
这是我得到的:
class Customer < ActiveRecord::Base
has_many :orders, (account) { where(:account_id => account.id) }
end
Run Code Online (Sandbox Code Playgroud)
但是,在我的控制器或视图中,我如何通过正确的帐户?在我执行以下代码时,请执行以下操作:
customers.orders
Run Code Online (Sandbox Code Playgroud)
我得到账户ID为1的所有订单,看似随意.
我对laravel范围部分有点困惑.
我有一个用户模型和表.
如何为用户分配用户,客户和/或管理员的角色.
我有一个带vue和laravel api后端的SPA.我使用https://laravel.com/docs/5.3/passport#consuming-your-api-with-javascript
Passport::tokensCan([
'user' => 'User',
'customer' => 'Customer',
'admin' => 'Admin',
]);
Run Code Online (Sandbox Code Playgroud)
如何指定哪个用户模型具有哪个范围?
或者范围与角色不同?
你会如何实现这个?
提前致谢!
我期待编写一个范围,返回所有没有特定关联的记录.
foo.rb
class Foo < ActiveRecord::Base
has_many :bars
end
Run Code Online (Sandbox Code Playgroud)
bar.rb
class Bar < ActiveRecord::Base
belongs_to :foo
end
Run Code Online (Sandbox Code Playgroud)
我希望有一个范围,可以找到所有的Foo's那不要有任何bars.很容易找到使用关联的那些joins,但我还没有找到相反的方法.
基本上我正在寻找一种适用于范围链的无操作类型的关系.
可以说我有一系列范围:
Post.approved.published.all
Run Code Online (Sandbox Code Playgroud)
现在,出于调试目的,我希望published范围根本不做任何事情,因此链只会返回approved帖子,无论它们是否published存在.
我将通过以下方法返回什么:
def self.published
# what to return?
end
Run Code Online (Sandbox Code Playgroud) 我偶然发现了一篇关于Rails 3+范围的精彩文章:http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly -named-范围/ index.html中
你可以在那里阅读(在'Crazy Town'部分),可以合并来自不同模型的范围,如下所示:
class User < ActiveRecord::Base
scope :published, lambda {
joins(:posts).group("users.id") & Post.published
}
end
Run Code Online (Sandbox Code Playgroud)
它按预期工作,并允许您这样做:
User.published.to_sql
#=> SELECT users.* FROM "users"
# INNER JOIN "posts" ON "posts"."author_id" = "users"."id"
# WHERE (posts.published_at IS NOT NULL AND posts.published_at <= '2010-02-27 02:55:45.063181')
# GROUP BY users.id
Run Code Online (Sandbox Code Playgroud)
我在我的Rails 3.1项目中尝试了这种方法,显然它已不再适用了.
所以我克隆了文章的Rails 3.0.0-beta1项目,看到我的眼睛说这些人没有说谎,事情按照他们说的方式工作.
然后我3.1了,现在我得到了:
ruby-1.9.2-p290 :003 > User.published.to_sql
User Load (0.3ms) SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."author_id" = "users"."id" GROUP BY users.id
Post Load …Run Code Online (Sandbox Code Playgroud) 是否可以将请求范围的bean自动装配到应用程序范围的bean中.即
我有一个RequestScopedBean类:
class RequestScopedBean {
....
....
....
}
Run Code Online (Sandbox Code Playgroud)
以及一个应用程序作用域bean,其中请求作用域bean是自动装配的.
class ApplicationScopedBean {
@Autowire
private RequestScopedBean requestScopedBean;
....
....
....
}
Run Code Online (Sandbox Code Playgroud)
和spring-config xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
">
<bean id="applicationScopedBeans" class="ApplicationScopedBean" />
<bean id="requestScopedBean" class="RequestScopedBean" scope="request">
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
当我尝试运行此应用程序时,applicationScopedBean的bean创建失败,并出现以下错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ApplicationScopedBean': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private RequestScopedBean requestScopedBean; nested exception is java.lang.IllegalStateException: No Scope registered for …Run Code Online (Sandbox Code Playgroud) 我做了一个松散的应用程序,我有一个机器人.我选择了频道:历史,频道:阅读,频道:在我的许可范围内写作,我也通过了范围
"scope":"bot channel:history channel:read channel:write"
在进行我的oauth2验证(使用python)时,我得到了范围的响应
"scope":"identify,bot,channels:history,channels:read,channels:write"
Run Code Online (Sandbox Code Playgroud)
以及客户端和机器人访问令牌.
现在,当我进行api调用以获取我的机器人被邀请的频道的历史记录时,我得到了
{
ok: false,
error: 'missing_scope',
needed: 'channels:history',
provided: 'identify,bot:basic'
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我哪里出错了.我怎样才能PROVIDE渠道:历史范围在我的API调用.这真让我疯了.我应该使用客户端访问令牌,即xoxp-xxxx,而不是机器人令牌,即xoxb-xxxx?
谢谢!
我想使用 Keycloak 来授权访问我的 API。
我已经定义了相关范围,并且这些范围按预期返回到访问令牌中:
{
... claims ...
"scope": "openid profile user/Patient.write user/Patient.read",
... etc ...
}
Run Code Online (Sandbox Code Playgroud)
但托管 API 的服务器需要以数组表示的访问令牌中的范围,如下所示:
{
... claims ...
"scope": [
"openid",
"profile",
"user/Patient.read",
"user/Patient.write"
],
...etc...
}
Run Code Online (Sandbox Code Playgroud)
我在 Keycloak 中看不到任何可以更改行为以将范围输出为数组的地方?
我已经考虑过使用自定义令牌映射器脚本来执行此操作,但范围似乎在映射器脚本中不可用,因此看起来我无法以这种方式重新映射它们。有什么方法可以将 Keycloak 中令牌的范围转换为这种形式吗?
我将从rubykoans教程中显示您的代码片段.考虑下一个代码:
class MyAnimals
LEGS = 2
class Bird < Animal
def legs_in_bird
LEGS
end
end
end
def test_who_wins_with_both_nested_and_inherited_constants
assert_equal 2, MyAnimals::Bird.new.legs_in_bird
end
# QUESTION: Which has precedence: The constant in the lexical scope,
# or the constant from the inheritance hierarchy?
# ------------------------------------------------------------------
class MyAnimals::Oyster < Animal
def legs_in_oyster
LEGS
end
end
def test_who_wins_with_explicit_scoping_on_class_definition
assert_equal 4, MyAnimals::Oyster.new.legs_in_oyster
end
# QUESTION: Now which has precedence: The constant in the lexical
# scope, or the constant from the inheritance hierarchy? …Run Code Online (Sandbox Code Playgroud) scopes ×10
activerecord ×2
associations ×2
oauth-2.0 ×2
arel ×1
class-method ×1
keycloak ×1
laravel ×1
laravel-5.3 ×1
nested-class ×1
ruby ×1
slack ×1
slack-api ×1
spring ×1
sql ×1