是否可以named_scope为某列提供唯一的返回记录?
例如
named_scope :unique_styles, :order =>"title desc", :limit => 3
Run Code Online (Sandbox Code Playgroud)
这会给我三种风格,但如果我想确定标题不同怎么办?在这种情况下,可能有三个具有相同样式的记录,我希望这个named_scope只给出标题的唯一值.
所以["style 1", "style 1", "style 1"]不可能,它会强迫自己给予["style 1", "some style 2", "maybe another 3"]
group可能会这样做,而我现在正在使用它.如果有人有任何评论,不管这是不是很好.我看到问题的本质
有一天,如果我没弄错的话,我已经看到了重用named_scope来定义另一个named_scope的例子.像这样的东西(不记得确切的语法,但这正是我的问题):
named_scope :billable, :conditions => ...
named_scope :billable_by_tom, :conditions => {
:billable => true,
:user => User.find_by_name('Tom')
}
Run Code Online (Sandbox Code Playgroud)
问题是:如果可能的话,确切的语法是什么?我找不到它,谷歌也没有帮助.
一些解释
为什么我真的想要它,是因为我使用Searchlogic来定义一个复杂的搜索,这可能会产生如下表达式:
Card.user_group_managers_salary_greater_than(100)
Run Code Online (Sandbox Code Playgroud)
但到处都是太长了.因为据我所知,Searchlogic只是动态定义了named_scopes,我想在Card类上设置一个named_scope,如下所示:
named_scope from_big_guys, { user_group_managers_salary_greater_than(100) }
Run Code Online (Sandbox Code Playgroud)
- 这是我在named_scope中使用那个漫长的Searchlogic方法的地方.但是,再次,语法是什么?无法弄清楚.
恢复
那么,named_scope嵌套(我不是指链接)实际上可能吗?
我有一个user.rb模型,我在其中定义了一个范围admins,即通过权限表具有admin角色的用户.
has_many :permissions
has_many :roles, :through => :permissions
Run Code Online (Sandbox Code Playgroud)
范围的工作方式如下:
scope :admins, joins(:permissions).merge(Permission.admin_permissions)
Run Code Online (Sandbox Code Playgroud)
我还想制作一个名为non-admins或类似的范围,这是所有没有管理员角色的用户.
最简单的方法是什么?
是的,我知道之前已经问过,是的,我知道这是"按设计".
但是我想做这样的事情:
@Component(modules = {RealmModule.class})
public interface RealmComponent {
Realm realm();
}
@Component(modules = {RepositoryModule.class})
public interface RepositoryComponent {
PersonRepository personRepository();
ScheduleRepository schedulesRepository();
}
@Component(dependencies = {RealmComponent.class, RepositoryComponent.class})
public interface AppDataComponent
extends RealmComponent, RepositoryComponent {
}
@ApplicationScope
@Component(dependencies = {AppContextComponent.class,
AppDataComponent.class,
AppDomainComponent.class,
AppPresentationComponent.class,
AppUtilsComponent.class})
public interface ApplicationComponent
extends AppContextComponent, AppDataComponent, AppDomainComponent, AppUtilsComponent, AppPresentationComponent {
void inject(CustomApplication customApplication);
void inject(DashboardActivity dashboardActivity);
}
Run Code Online (Sandbox Code Playgroud)
但是,我得到的是无范围的,每次我注入一个JobManager或一个ScheduleRepository或其他任何东西,我得到一个新的实例.我唯一可以"修复"的方法就是这样.
@Module
public class JobManagerModule {
private JobManager jobManager;
@Provides
public …Run Code Online (Sandbox Code Playgroud) 我有
class Foo < ActiveRecord::Base
named_scope :a, lambda { |a| :conditions => { :a => a } }
named_scope :b, lambda { |b| :conditions => { :b => b } }
end
Run Code Online (Sandbox Code Playgroud)
我想要
class Foo < ActiveRecord::Base
named_scope :ab, lambda { |a,b| :conditions => { :a => a, :b => b } }
end
Run Code Online (Sandbox Code Playgroud)
但我宁愿以干燥的方式做这件事.我可以通过使用获得相同的效果
Foo.a(something).b(something_else)
Run Code Online (Sandbox Code Playgroud)
但它并不是特别可爱.
我一直在努力开发一个基于电影的rails应用程序,它支持多个地区(好莱坞,宝莱坞等).我将多个区域称为应用程序中的语言.
每种语言都有自己的数据集,即英语中包含与好莱坞相关的所有电影,而语言印地语则包含与宝莱坞相关的所有电影.
语言模型
class Language < ActiveRecord::Base
has_many :movies
has_many :cast_and_crews, :through => :movies, :uniq => true
has_many :celebrities, :through => :cast_and_crews, :uniq => true
# FIXME: Articles for celebrities and movies
has_many :article_associations, :through => :celebrities
has_many :articles, :through => :article_associations, :uniq => true
end
Run Code Online (Sandbox Code Playgroud)
这里的电影和名人都有使用article_association类的文章.
电影模型
class Movie < ActiveRecord::Base
belongs_to :language
has_many :cast_and_crews
has_many :celebrities, :through => :cast_and_crews
has_many :article_associations
has_many :articles, :through => :article_associations, :uniq => true
end
Run Code Online (Sandbox Code Playgroud)
名人模特
class Celebrity < ActiveRecord::Base
has_many :cast_and_crews
has_many …Run Code Online (Sandbox Code Playgroud) scope named-scope ruby-on-rails associations ruby-on-rails-3
我有一种情况,现有应用程序的行为正在发生变化,这让我头疼.
我的应用有照片.照片有一个状态:"batch", "queue", or "complete".应用中的所有现有照片都是"完整的".
99%的时间我只需要显示完整的照片,而在所有现有的代码库中,我需要每次调用照片仅限于完整的照片.
但是,在与上传和分类照片相关的屏幕中,我需要能够相当轻松地覆盖默认范围以显示批量或排队的照片.
像许多其他人一样,我需要找到一种在某些情况下轻松覆盖默认范围的方法.我看着这些问题(1,2),他们似乎并没有回答我要找的.
我希望的代码是这样的:
class Photo < ActiveRecord::Base
...
default_scope where(:status=>'complete')
scope :batch, unscoped.where(:status=>'batch')
scope :queue, unscoped.where(:status=>'queue')
...
end
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.我尝试在lambdas中包装范围方法,但这也不起作用.
我知道default_scope带有行李,但是如果我不能使用覆盖,那么我正在寻找添加scope :complete ...并且必须梳理我现有应用程序中每次调用照片并添加.complete以过滤未处理的照片.
你会如何解决这个问题?
activerecord named-scope ruby-on-rails default-scope ruby-on-rails-3
这些方法是什么以及覆盖它们有多糟糕?
irb(main):001:0> Object::respond_to?('private', true)
=> true
irb(main):002:0> Object::respond_to?('public', true)
=> true
Run Code Online (Sandbox Code Playgroud)
在尝试为模型定义名为private或public的作用域时,Rails会出现此问题.由于修复了错误https://rails.lighthouseapp.com/projects/8994/tickets/4167-activerecord-named_scope-using-columns-as-the-name-is-buggered,现在有很多警告,例如:
Creating scope :public. Overwriting existing method MyModel.public.
Run Code Online (Sandbox Code Playgroud) 首先,我是rails的新手,如果有任何我不理解的话,那就很抱歉.我想知道如何通过API填充数据获取模型.
上下文:我正在使用omniauth/devise进行OAuth2身份验证.
在我的用户控制器客户端(与提供商相对),我获取了至少登录一次的所有用户是这个"客户端应用程序",我想显示它们.显然,每当新用户登录到客户端应用程序时,我都不会将其所有信息存储在客户端数据库中以避免重复.我正在存储的是user_id及其访问令牌.
因此,我想在获取所有用户数据后,我可以将它们填充到用户模型,然后再将其传递给视图.做这种事的最好方法是什么?我正在研究命名范围,但我不清楚如何将它应用于我的特定场景.例如,能够使用以下内容获取模型中的所有用户,这将是很棒的:
# ApiRequest is a container class for HTTParty
get = ApiRequest.new.get_users(User.all) // here "get" is a JSON array of all users data
response = get.parsed_response['users']
User.populate(response).all # would something like this possible? If yes, is Named Scope the appropriate solution?
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助.
我有一个用户模型.我可以检查用户是否是管理员a_user.try(:admin?).
我想定义一个命名范围,让所有用户在最近X分钟内更新,而不是管理员.到目前为止,我有:
scope :recent, lambda { { :conditions => ['updated_at > ?', 5.minutes.ago] } }
Run Code Online (Sandbox Code Playgroud)
这成功地使所有用户在最近5分钟内更新,但如何合并管理员检查?我不知道如何try()在范围内调用用户的实例...
named-scope ×10
activerecord ×5
scope ×4
ruby ×2
android ×1
associations ×1
dagger-2 ×1
model ×1
rails-api ×1
rails-models ×1
searchlogic ×1