Questions tagged [named-scope]

339 questions
4
votes
2 answers

Mixing acts_as_tree (ancestry gem), acts_as_list and default model scoping

I'm using the ancestry gem to structure some groups in a tree. At the same time I'm using acts_as_list to keep groups at the same tree level in a sorted list. Given the following model: class Group < ActiveRecord::Base acts_as_tree acts_as_list…
4
votes
1 answer

Can I create an *un*named scope in Rails?

I know you can create named scopes in Rails, which allow you to specify conditions which can then be built on later: named_scope :active, :conditions => {:active => true} ... MyModel.active.find(...) This works by creating a proxy object which…
Gareth
  • 133,157
  • 36
  • 148
  • 157
4
votes
2 answers

How do I make named_scope work properly with a joined table?

Here's my situation. I have two tables: pledges and pledge_transactions. When a user makes a pledge, he has only a row in the pledges table. Later when it comes time to fulfill the pledge, each payment is logged in my pledge_transactions table. I…
rwl4
  • 267
  • 1
  • 3
  • 14
4
votes
1 answer

will paginate miscounting oddness with named scopes

I recently broke up my query into 4 named scopes to make it easier to re-sort and will paginate which otherwise always worked fine now has problems calculating the number of pages. named_scope :loc, lambda { |id| { :conditions => ['location_id =…
holden
  • 13,471
  • 22
  • 98
  • 160
3
votes
1 answer

Variable field name in named_scope?

In a Rails model I am trying to acheive a named_scope that filters on a start_date and end_date. This is easy. But I am going to have to do it on lots of different fields on many occasions. Is this asking for trouble? If so why (SQL injection?) and…
nitecoder
  • 5,496
  • 1
  • 28
  • 35
3
votes
1 answer

Nested scopes in Rails 3.1

Is there a way that I can convert the to_be_approved by method in the following code to be a scope that uses the two other scopes? scope :reports_to, lambda { |manager| joins(" JOIN admin_users request_user on requests.admin_user_id =…
Dennis Burton
  • 3,282
  • 3
  • 23
  • 30
3
votes
1 answer

Rails has_many with finder_sql and name_scope in combination return nil

For example lets say you have: class Model < AR::Base has_many :somethings, :finder_sql => "SELECT * FROM somethings" end class Something < AR::Base named_scope :valuable {...code...} end #…
Filip
  • 712
  • 1
  • 6
  • 14
3
votes
1 answer

Two parameters for Rails3 scope

Currently i have two models: Rate and Item Rate is a model for voting, and there is votes and player_id Rate has_many :votes Vote belongs_to :rate Also, for Item model, currently i have a scope like: scope :pop, proc { order('votes.votes DESC')…
There Are Four Lights
  • 1,406
  • 3
  • 19
  • 35
3
votes
2 answers

Named scopes with inheritance in Rails 3 mapping to wrong table

I am attempting to use inheritance from a class which has a named scope: Class A < ActiveRecord::Base scope :useful_scope, lambda { |value1, value2| where(:value1 => value1, :value2 => value2) end end Class B < A set_table_name…
Ben Zittlau
  • 2,345
  • 1
  • 21
  • 30
3
votes
2 answers

Dynamic `named_scope` depending on certain criterias

dear all, i have a Student model that i've specified some name_scope in it, e.g. from_program, from_year, from_school, has_status, from_course, etc... is there anyway that i can chain the different named_scope together dynamically depending on…
Staelen
  • 7,691
  • 5
  • 34
  • 30
3
votes
0 answers

Problem with Thinking Sphinx and scopes

For several hours now I am unsuccessfully trying to get sphinx scopes work. I want to scope tags of ActsAsTaggableOn. In my model (that is taggable) I tried the following scopes: # This normal scope works scope :tagged, lambda { joins(:taggings =>…
medihack
  • 16,045
  • 21
  • 90
  • 134
3
votes
2 answers

How do I write a named scope to filter by all of an array passed in, and not just by matching one element (using IN)

I have two models, Project and Category, which have a many-to-many relationship between them. The Project model is very simple: class Project < ActiveRecord::Base has_and_belongs_to_many :categories scope :in_categories, lambda { |categories| …
3
votes
1 answer

Chaining Named Scopes not working as intended

I have 2 simple named scopes defined as such: class Numbers < ActiveRecord::Base named_scope :even, :conditions => {:title => ['2','4','6']} named_scope :odd, :conditions => {:title => ['1','3','5']} end if I call Numbers.even I get back 2,4,6…
Cheezy
  • 41
  • 2
3
votes
1 answer

Using named_scopes on the join model of a has_many :through

I've been beating my head against the wall on something that on the surface should be very simple. Lets say I have the following simplified models: user.rb has_many :memberships has_many :groups, :through => :memberships membership.rb belongs_to…
uberllama
  • 18,752
  • 2
  • 16
  • 8
3
votes
3 answers

ruby on rails named scope implementation

From the book Agile Web Development With Rails class Order < ActiveRecord::Base named_scope :last_n_days, lambda { |days| {:conditions => ['updated < ?' , days] } } named_scope :checks, :conditions => {:pay_type => :check} end The…
Heinrich Lee Yu
  • 1,472
  • 15
  • 31