Questions tagged [named-scope]

339 questions
0
votes
1 answer

How to use variable named scopes with attributes of associated models OTHER than id

I'm having trouble with getting a named scope working using using an attribute of the associated model other than the id column. I have a Firm model which has a city_id column. I also have a City model with a name column. I want to get restful urls…
Jack Kinsella
  • 4,491
  • 3
  • 38
  • 56
0
votes
1 answer

good syntax for ruby scope

I just wanna know if there is a way of doing this : I want an "invited" method for my user class to create a list of user(s) that have their referral_code equals to the self.confirmation_token I tried a lot of things and the last thing was almost…
Charlon
  • 363
  • 2
  • 16
0
votes
1 answer

warning when using a named_scope as part of an anonymous scope

I have the following named scope: named_scope :find_all_that_match_tag, lambda { |tags| { :select => "articles.id, tags.name", :joins => :tags, :conditions => ["tags.name IN (?)",tags]} } It works fine…
0
votes
1 answer

How to translate this MySQL statement into named_scope method?

"select * from users, awards where (users.id = awards.user_id) and awards.trophy_id not in (select awards.trophy_id from awards where awards.trophy_id = #{trophy.id})"
keruilin
  • 16,782
  • 34
  • 108
  • 175
0
votes
1 answer

PGError syntax problem for named_scope

I have the following named_scope which works fine in MySQL and sqlite but bombs in Postgres: course.rb named_scope :current, :conditions => ['start < ? AND end > ? ', Time.now, Time.now], :order => 'start ASC' Then I just call: Course.current I…
0
votes
1 answer

Rails: handling two paged lists on single view

Assuming following model: class Account < ActiveRecord::Base has_many :orders end class Order < ActiveRecord::Base belongs_to :account scope :active, -> { where('orders.state = ?', 'ACTIVE') } scope :closed, -> { where('orders.state <> ?',…
Michael D.
  • 1,249
  • 2
  • 25
  • 44
0
votes
2 answers

Ruby on Rails - Implementing Simple Search with scopes

I followed the Railscasts #37, very interesting. I tried to apply it to a search that already implies a scope and pagination. But it failed unsuccessfully. Being quite new at rails, I wonder if a better solution exists. Here is my original…
user1185081
  • 1,898
  • 2
  • 21
  • 46
0
votes
2 answers

aggregate conditional has_many rails association

I'm not sure if this is feasible or even preferable But I'd like to build a has_many relationship or a conditional named_scope type relationship to ease my pain into one simple relationship instead of two. Currently I have two models which basically…
holden
  • 13,471
  • 22
  • 98
  • 160
0
votes
1 answer

Issues with rails, postgres, timezones and scopes

We're seeing some funky stuff when trying to do scopes in Rails 3 in regards to timezones. Let's say we have an attribute called start_at of type date time. In application.rb we have config.time_zone = 'Eastern Time (US & Canada)' set based on our…
JoshL
  • 1,397
  • 1
  • 12
  • 28
0
votes
1 answer

Nested scopes with rails 4

I have 2 scopes: with_category and simple_search. Each of them works well individually but together I can't get exactly what I need. product.rb has_many :categorizations, :dependent => :destroy has_many :categories, :through =>…
Mike Andrianov
  • 3,033
  • 3
  • 23
  • 24
0
votes
1 answer

dynamically set named_scope based on current_user

I keep getting the following error: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.size Based on the current user, when they navigate to a page, I want…
JohnMerlino
  • 3,900
  • 4
  • 57
  • 89
0
votes
1 answer

Nested named scopes with joins (explosive error)

So I have an ActiveRecord class with a couple different named scopes that include join parameters. While running a report, I happen to have a situation where one gets called inside of the other: 1 Model.scope_with_some_joins.find_in_batches do…
0
votes
2 answers

How to return boolean result in named_scope?

named_scope :incomplete?, lambda { |user_id, todo_id| { :select => 1, :conditions => [ "#{user_id} not in (select user_todos.user_id from user_todos) and #{todo_id} not in (select user_todos.todo_id from user_todos)" ] } } I'm…
keruilin
  • 16,782
  • 34
  • 108
  • 175
0
votes
0 answers

Fixing memory leak of EF4.1 context + Ninject combo

I have a windows service which periodically runs some jobs using NCron. I recently introduced Ninject and got some serious memory issues due to the fact that the EF context (ObjectContext from EF4.1) is not GC'ed (I memory-profiled it). It might be…
0
votes
1 answer

Composing named_scopes that limit on the same field

In Rails 2.3, I have the following model: class Foo named_scope :first_scope, lambda { record_ids = SomeModel.find(some_conditions).map(&:foreign_ids) { :conditions => {:field => record_ids} } } named_scope :second_scope, lambda { …
wmjbyatt
  • 686
  • 5
  • 15