Questions tagged [named-scope]

339 questions
7
votes
1 answer

SQLAlchemy equivalent of named scopes in ActiveRecord

Is there an equivalent of AR's named scopes? Named scopes are basically filters, that can be wrapped in a method, and then chained. Here's an example from…
Vlad Zloteanu
  • 8,464
  • 3
  • 41
  • 58
6
votes
3 answers

How to have multiple conditions in a named scope?

I have a User model. I can check whether a User is an admin by doing a_user.try(:admin?). I'd like to define a named scope that gets all Users updated within the last X minutes who are not admins. So far I have: scope :recent, lambda { { :conditions…
XåpplI'-I0llwlg'I -
  • 21,649
  • 28
  • 102
  • 151
5
votes
2 answers

is there a way to mix rails named scopes and thinking sphinx scopes?

Is there a way to mix named scopes and sphinx scopes? I can't believe there's not. I have sphinx index on Lessons. Once users purchase lessons, they have a screen that they can see a list of all of their lessons. I want them to perform the same…
pixelearth
  • 13,674
  • 10
  • 62
  • 110
5
votes
0 answers

Dagger 2 fragments with different scope

Is it possible to have two fragments from different scope in same activity where activity is in same scope as one of the fragments. I wrote sample code here (https://github.com/damirmiljkovic/daggerimplementation) which you can check it out. What I…
Damir
  • 1,092
  • 4
  • 18
  • 32
5
votes
2 answers

Returning array of objects via named_scope -- has_many...belongs_to association; UNION ALL query

I'm looking for an answer that will return an array of user objects via (preferably) a named_scope or via a class method on the User model that does some manipulation. So without further ado... I have two tables: users and fights. User has many…
keruilin
  • 16,782
  • 34
  • 108
  • 175
5
votes
1 answer

Sorting a Rails database table by a column in an associated model

I'm trying to implement Ryan Bates' sortable table columns code (Railscast #228) but I'd like to be able to sort on an associated column. In particular, I have the following models and associations: class Project < ActiveRecord::Base belongs_to…
hikari17
  • 143
  • 1
  • 2
  • 6
5
votes
1 answer

Why does Rails generate duplicate SQL conditions when using scopes with associations?

I have setup a model structure which allows different models to associate with a File model through a has_many ... :through ... association which is also polymorphic so that a File can belong to many different models and different Resources can have…
DanneManne
  • 21,107
  • 5
  • 57
  • 58
5
votes
1 answer

How to get the currently active tf.variable_scope in TensorFlow?

I wrote a convenience API to define my models in TensorFlow that exposes a scope object. Now I want to verify that the user actually enters the scope before adding operations. How to get the name of the currently active tf.variable_scope in…
danijar
  • 32,406
  • 45
  • 166
  • 297
5
votes
2 answers

Combining multiple named scopes with OR

I am trying to combine two scopes or add some more to an existing scope. scope :public_bids, -> { where(status: [Status::ACCEPTED, Status::OUTBID, Status::SOLD, Status::NO_SALE], bid_type: [BidType::MANUAL, BidType::AUTO,…
Zack Herbert
  • 942
  • 1
  • 16
  • 39
5
votes
2 answers

Rails Workflow Gem - Metaprogramming events into named_scopes?

I'm using http://github.com/geekq/workflow to provide a state machine. I'm using ActiveRecord to save state, which means I have a "workflow_state" attribute in the model. I think I want a named_scope for each event in the state machine, so I can…
JezC
  • 1,868
  • 17
  • 19
4
votes
1 answer

Alternative to default_scope in ActiveRecord

I'm facing a problem with changing the functionality of an app and having to re-write about 700 method calls that now need to be scoped. I've been looking into how default_scope works, and like most people I'm finding that it's close but not quite…
Andrew
  • 42,517
  • 51
  • 181
  • 281
4
votes
3 answers

named_scope and .first?

I can return a collection of objects, with only one (:limit => 1) but is there a way to return the .first() object only, like not within a collection? named_scope :profile, :conditions => {:association => 'owner', :resource_type => 'Profile'},…
Andrew Lank
  • 1,607
  • 1
  • 15
  • 29
4
votes
2 answers

Rails order based on association

I have a Problem and a Solution model. Problem has many solutions and Solution belongs to problem. I need to get the recently solved problem, that means, get the last X solutions and get it's problem. I'm pretty sure there's a way to do this with…
Hock
  • 5,784
  • 2
  • 19
  • 25
4
votes
1 answer

Issues chaining scopes in Rails with context

I have 4 models: Artists, Editions, Events & EventItems. Editions have many Events Events have many Artists through EventItems Editions have many Artists through Events On artists I have a scope published I define active as an artists who is on an…
ere
  • 1,739
  • 3
  • 19
  • 41
4
votes
2 answers

Using named_scope with counts of child models

I have a simple parent object having many children. I'm trying to figure out how to use a named scope for bringing back just parents with specific numbers of children. Is this possible? class Foo < ActiveRecord::Base has_many :bars …
1 2
3
22 23