Questions tagged [named-scope]

339 questions
0
votes
1 answer

has_many and sum named_scope

I have this situation: Stories has many Tasks Tasks have an integer called hours_left I need a named scope to find Stories which all its tasks has more than 0 hours left. Based on this post. I wrote this: class Story has_many :tasks named_scope…
Manuel M
  • 469
  • 4
  • 13
0
votes
0 answers

Add a uniqueness constraint to a model's named_scope

I have this configuration: model Box < ActiveRecord::Base has_many :toys has_many :inventories, :through => :toys scope :for_inventory, lambda{ |inv| joins(:toys).where("toys.inventory_id = ?",inv) } end model Toy < ActiveRecord::Base …
muichkine
  • 2,890
  • 2
  • 26
  • 36
0
votes
2 answers

Name scope for finding emails from table field

In the user table I have a name field, where some records are names, and some are emails. I know, bad approach. That aside. I'd like to write a named scope that returns only email records. I've tried adapting a query that does this query, and…
jahrichie
  • 1,215
  • 3
  • 17
  • 26
0
votes
1 answer

Scope staticly reference another model resulting table not found

I use in-memory database for testing. The schema is reloaded in every test. Recently my rspec complains that a table is not found. The reason is that a scope is referencing another model at load time. class Item scope :public, where(:store_id =>…
lulalala
  • 17,572
  • 15
  • 110
  • 169
0
votes
1 answer

Complicated named_scope: Find users who don't belong to a certain project

I'm trying to create a named scope like User.not_in_project(project) but I can't find the right way. I have Users, Projects and Duties as a join model: class User < ActiveRecord::Base has_many :duties, :extend => FindByAssociatedExtension …
Manuel M
  • 469
  • 4
  • 13
0
votes
1 answer

Rails :scope across databases

I have three models, Alarms, Sites, Networks. They are connected by belongs_to relationships, but they live in diferent databases class Alarm < ActiveRecord::Base establish_connection :remote belongs_to :site, primary_key: :mac_address,…
0
votes
3 answers

Initialize instance variable through named scope

Is it possbile to initialize an instance variable through a named scope? I want to remember my search criteria after the initial search. See the following: class Foo < ActiveRecord::Base attr_accessor :search named_scope :bar, lambda…
user1454117
0
votes
1 answer

Find all associated objects by specific condition

class QuestionGroup < ActiveRecord::Base has_many :questions end class Question < ActiveRecord::Base belongs_to :question_group has_many :question_answers has_many :question_users_answers, :through => :question_answers, :source =>…
zolter
  • 7,070
  • 3
  • 37
  • 51
0
votes
1 answer

Rails Testing a named_scope with a date range

SCENARIO I have a named_scope on a model called 'last_week'. All it does is it fetches the records from last week. I want to test this and my approach is to test that the results coming back are within a certain range. I don't know if this is the…
alenm
  • 1,013
  • 3
  • 15
  • 34
0
votes
0 answers

How do I turn this ActiveRecord query into a named scope?

I'm trying to implement a trending feature on my app. I have a table called Search that has a keyword column. Every time a user uses the search function, the keyword is stored in the table as a row. What I did is I get the searches done for the past…
Terence Ponce
  • 9,123
  • 9
  • 31
  • 37
0
votes
1 answer

Rails - combining scopes from different tables

I have a couple of scopes on different (and somewhat linked) models class Alarm has_one :site #use mac_address field in Alarm and Site as key / foreign key scope :any_network, lambda{ joins(:network) } #more complex stuff can be done :-) …
0
votes
3 answers

problem using sql instead of named scope in rails

I have a method "search" in my model, which depending upon the various parameters passed runs an sql query in which i am joining seven tables. but when i am using this method with another named scope then error is shown "undefined method call for…
mohit
  • 1,913
  • 3
  • 16
  • 23
0
votes
1 answer

Add named_scopes dynamically in rails application

Just like dynamic finder methods in rails, is there any way to have dynamic finder methods for associated models? Consider the following models class User attr_accessible :name, :phone_no has_many :notes end class Note belongs_to :user …
Kashif Umair Liaqat
  • 107
  • 1
  • 1
  • 11
0
votes
2 answers

Is there a way of doing filtering joined associations using named scope?

I have the following associated models class Enrollment < ActiveRecord::Base has_many :addresses end class Address < ActiveRecord::Base belongs_to :address_type end Currently I'm using the following (which I think is ugly) to filter out…
JasonOng
  • 3,268
  • 3
  • 21
  • 17
0
votes
2 answers

what is the difference between named_scope and method?

named_scope or scope how difference with class method. named_scope :active, :conditions => {:status => 'Active'} def self.active self.find(:all, :conditions => {:status => 'Active'} end Whats the difference between the two?
1 2 3
22
23