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 database record by a key, return a default if not found in database.
Even better if can...
how can I write a NOT IN in named scope syntax? For example, User :has_many Photos, how can I define:
User.has_no_photo
and returns all users who are not in the Photo model? thanks!
...
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 which is correct
if I call Numbers.odd I get back 1,3,5 which is correct
When I chain t...
I'm using Searchlogic to search on many fields in a database. One of those fields is a :has_may, :through => relationship, and I can't get it to work.
Here are the relevant parts of the models:
Source.rb:
class Source < ActiveRecord::Base
has_many :authorships
has_many :authors, :through => :authorships
end
Authorship.rb:
clas...
Two models (Rails 2.3.8):
User; username & disabled properties; User has_one :profile
Profile; full_name & hidden properties
I am trying to create a named_scope that eliminate the disabled=1 and hidden=1 User-Profiles. Moreover, while the User model is usually used in conjunction with the Profile model, I would like the flexibility to...
I'm trying to tidy up my code by using named_scopes in Rails 2.3.x but where I'm struggling with the has_many :through associations. I'm wondering if I'm putting the scopes in the wrong place...
Here's some pseudo code below. The problem is that the :accepted named scope is replicated twice... I could of course call :accepted something...
Two models (Rails 2.3.8):
User; username & disabled properties; User has_one :profile
Profile; full_name & hidden properties
I am trying to create a named_scope that eliminate the disabled=1 and hidden=1 User-Profiles. The User model is usually used in conjunction with the Profile model, so I attempt to eager-load the Profile model...
I've got a named scope for one of my models that works fine. The code is:
named_scope :inbox_threads, lambda { |user|
{
:include => [:deletion_flags, :recipiences],
:conditions => ["recipiences.user_id = ? AND deletion_flags.user_id IS NULL", user.id],
:group => "msg_threads.id"
}}
This works fine on my local copy of the app wit...
I have a model "Product" with a "description" field. Now I want to have a link in the index page that when clicked will show all products where the description is blank (empty).
In the model I have defined a named_scope like this
named_scope :no_description, :conditions => { :description => "" }
I have checked that the named_sco...
PROBLEM:
I want to run a query which would trigger something like
select * from users where code in (1,2,4);
using a named_scope.
WHAT I TRIED:
This is for a single code:
named_scope :of_code, lambda {|code| {:conditions => ["code = ?", code]}}
I tried something like
named_scope :of_codes, lambda {|codes| {:conditions => ...
I only want to find the records that aren't null or empty/blank, currently I have;
named_scope :in_gallery, :conditions => ["gallery IS NOT NULL"]
(gallery is a string) but if the user inputs then deletes a title then this empty string is included in the results.
to clarify I want to be able to select only the entries where the field...
I have a Model which I am using to track permissions in a hierarchical organization using the awesome_nested_set plugin. I'm running into a problem where two named_scopes, when chained together, are creating a duplication INNER JOIN.
class Group < ActiveRecord::Base
acts_as_nested_set
has_many :memberships
has_many :accounts, :thr...
I have the following named_scope:
named_scope :commentors, lambda { |*args|
{ :select => 'users.*, count(*) as total_comments',
:joins => :comments,
:conditions => { :comments => { :public_comment => 1, :aasm_state => 'posted', :chalkboard_user_id => nil} },
:group => 'users.id',
:having => ['coun...
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|
joins(:categories).
where("categories.id in (?)", categories.collect(&:to_i))
}
...
I must admit, I am not even sure if I put the question right...
In my app I have a bunch of named scopes to build more efficient finds.
The one that I can't get to work is this:
=> I want to find all products in the current category and its descendants. I use the 'ancestry' gem to build the tree, and it provides named scopes on the Cla...
Here is the setup:
end_date = DateTime.parse('2010-01-01 12:00:00-04')
and sometimes it's initialized:
end_date = 1.days.ago
The question... do these named_scope(s) generate the same EXACT SQL?
named_scope :before, lambda { |end_date|
{ :conditions =>
["overdraft_transaction_payments.created_at < ?", end_date] }...
What is a difference between named_scope and named_scope + lambda Ruby on Rails code statements?
named_scope :with_avatar, :conditions => ['avatar IS NOT NULL']
and
named_scope :date_from, lambda { |date| { :conditions => ['created_at >= ?', DateTime.strptime(date, '%d.%m.%Y')] } }
...
I have a model which has a field called deleted, which is used to mark those deleted items.
So normally I would just want to query those having deleted = false items, and in some special cases to list those deleted items for restoring.
Is it possible to do that? What I could do now is just using a named scope having :conditions => {:de...
I would like to have a named_scope for blogs with zero posts.
The following does not work.
class Post < ActiveRecord::Base
belongs_to :blog
end
class Blog < ActiveRecord::Base
has_many :posts
named_scope :has_no_posts, :conditions => "blogs.id NOT IN (SELECT blog_id FROM posts)"
end
...
I have the following models to associate users with roles:
class User < ActiveRecord::Base
has_many :user_role_assignments, :dependent => :destroy
has_many :user_roles, :through => :user_role_assignments
end
class UserRole < ActiveRecord::Base
has_many :user_role_assignments
has_many :users, :through => :user_role_assignments
e...