Questions tagged [named-scope]

339 questions
0
votes
4 answers

ruby one-liner for this possible?

Any chance the 2nd and 3rd lines can be combined in an one-liner and hopefully save one valuable? def self.date_format record = find_by_key('strftime') record ? record.value : "%Y-%b-%d' end the above function in a Config model try to fetch a…
ohho
  • 50,879
  • 75
  • 256
  • 383
0
votes
1 answer

Rails add scope to Mongoid criteria

I have a model with different scopes class Contact include Mongoid::Document scope :active scope :urgent scope :no_one_in_charge In some of my controller, I pull the active scopes my_controller.rb def my_action @contacts =…
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
0
votes
1 answer

How to Resolve MySQL Error? Chaining Named Scopes

I'm trying to chain two named_scopes in my User model. The first: named_scope :commentors, lambda { |*args| { :select => 'users.*, count(*) as total_comments', :joins => :comments, :conditions => { :comments => {…
keruilin
  • 16,782
  • 34
  • 108
  • 175
0
votes
1 answer

How to translate find_by_sql statement into named_scope?

How do I translate the following into a named_scope? def self.commentors(cutoff=0) return User.find_by_sql("select users.*, count(*) as total_comments from users, comments where (users.id = comments.user_id) and (comments.public_comment…
keruilin
  • 16,782
  • 34
  • 108
  • 175
0
votes
2 answers

executing named_scoped only when there are present params

Hi have a model like this: class EventDate < ActiveRecord::Base belongs_to :event named_scope :named, lambda { | name | { :joins => { :event => :core}, :conditions => ["name like ?", "%#{ name }%"] }} named_scope :date_range, lambda…
Luca Romagnoli
  • 12,145
  • 30
  • 95
  • 157
0
votes
2 answers

Creating names scoped based on join

I need filter 'apps' in my rails project based on whether they support iphone. I am trying to create a named scope for this purpose but I am getting the following error. app/models/app.rb:21: syntax error, unexpected '}', expecting…
Antarr Byrd
  • 24,863
  • 33
  • 100
  • 188
0
votes
1 answer

How to call a scope multipile times with just one database call

I have a database with Product and product has a name attribute which I want to search. Im using a scope to do that: scope name_contains, lambda{|q| where("name LIKE ?", "%#{q}%")} which works fine when I search for "Awesome Product" and the…
davidb
  • 8,884
  • 4
  • 36
  • 72
0
votes
1 answer

Use named_scope to find number of associated rows (Ruby on Rails + Searchlogic question)

Let's say I have: class ForumTopic < ActiveRecord::Base has_many :forum_posts named_scope :number_of_posts, ?????? end class ForumPost < ActiveRecord::Base belongs_to :forum_topic end What should I put in ????? to allow searchlogic query…
jaycode
  • 2,926
  • 5
  • 35
  • 71
0
votes
1 answer

Translate SQL statement into named_scope?

How can I translate this SQL into a named_scope? Also, I want the total comments param to be passed through a lambda. "select users., count() as total_comments from users, comments where (users.id = comments.user_id) and (comments.public_comment =…
keruilin
  • 16,782
  • 34
  • 108
  • 175
0
votes
1 answer

Is there a way to get this scope to return results in batches

I have this scope. I would like to have it set up to return batches of 25. scope :get_some_stuff, lambda { select(QUERY_SELECT). joins(QUERY_JOINS). …
bonum_cete
  • 4,730
  • 6
  • 32
  • 56
0
votes
1 answer

Turn 'if' statement into a named scope

<% @training_dates.reverse.each do |t| %> <% if t.candidate_limit != Subscription.group(:training_date_id).count[t.id] %> <%= t.name %> <% end %> <% end %> I want to turn this if statement into a scope so I can do…
cccvandermeer
  • 317
  • 2
  • 14
0
votes
2 answers

named_scope or find_by_sql?

I have three models: User Award Trophy The associations are: User has many awards Trophy has many awards Award belongs to user Award belongs to trophy User has many trophies through awards Therefore, user_id is a fk in awards, and trophy_id is a…
keruilin
  • 16,782
  • 34
  • 108
  • 175
0
votes
2 answers

Passing arguments to scope_procedure in searchlogic

I'd like to use searchlogic's scope_procedure feature like so class MyModelObject < ActiveRecord::Base scope_procedure :my_scope_proc, lambda { |p1, p2| { :conditions => "p1 >= #{p1} AND p2 < #{p2}" }} end Then, I am doing the search: scope =…
GregK
  • 1,653
  • 2
  • 15
  • 19
0
votes
1 answer

Design: How to declare a specialized memory handler class

On an embedded type system, I have created a Small Object Allocator that piggy backs on top of a standard memory allocation system. This allocator is a Boost::simple_segregated_storage<> class and it does exactly what I need - O(1) alloc/dealloc…
Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
0
votes
1 answer

How to do a "join" with an anonymous scope in ruby

Hey guys (and girls ^^) !!! Does sommebody knows how to do a "join" with an anonymous scope in ruby ??? With a named scope you just have to add ":joins =>....." but i can't really find the way to do it with anonymous ones ... . Thx in advance for…