I want to ensure that only Movies with a watched date are brought back and ordered by that value in ascending order.
I think I'm fairly close with the following:
default_scope :conditions => { :watched_date => not null },
:order => 'watched_date ASC'
But I don't know how to add a condition for a non-null date.
Does it work if you change the conditions to this?:
:conditions => 'watched_date IS NOT NULL'
You might want to include the table name incase this is used in joins like so:
:conditions => 'movies.watched_date IS NOT NULL'
Related
I'm trying to order my default_scope in a way that the itens with the boolean important = true show first, and all order by created_at desc.
So, I have the following code:
default_scope order_by(:important => :desc, :created_at => :desc)
But, looks like important field order is ignored.
How can I made it work?
Thanks in advance
EDIT:
I just shift the order of the order params and it works:
default_scope order_by(:created_at => :desc, :important => :desc)
Just that simple.
This is an example on how to sort two columns in rails (probably you might have to slightly modify it to match your requirement), But I think you get the idea
<Model>.all(:order => 'important, created_at')
HTH
What I would like to find is all Events, where event.event_date is in the future, and only get the top 3 events sorted by how many Users the event has associated with it. Events and Users are joined by a HABTM relationship. Here's what I tried:
#popular_events = Event.where("event_date >= ?", Time.now)
.find(:all,
:joins => :users,
:group => 'event_id',
:order => "users.count DESC",
:limit => 10 )
I've tried a few other things with no luck. It is saying users.count is not a valid column.
believe it or not, this is a pain in the b*tt to do this with ActiveRecord. You will more or less have to rely on raw SQL to do it. Some answers here : http://www.tatvartha.com/2009/03/activerecord-group-by-count-and-joins/
I have the following models:
User (id)
UserRoom (user_id, room_id, banned(boolean)
Room (id, uuid)
Right now I can get all of a user's rooms as follows:
current_user.rooms.find_all_by_uuid(#requested_ids)
What I would like to do is expand this to only show rooms that aren't banned (banned => false).
What's the right rails way to add that condition to the query?
Thanks
Try using conditions like so:
current_user.rooms.find :all, :conditions => { :uuid => #requested_id, :banned => false }
Using #requested_ids as an array (probably not as elegant):
current_user.rooms.find :all, :conditions => ["uuid IN (?) AND banned=0", #requested_ids]
For example, say I have something like BlogCategory which has a HABTM with BlogPost, and I want to select only the BlogCategories that have actually been used in a BlogPost
named_scope :published, {
:include => :blog_posts,
:select => 'blog_categories.*, count(blog_posts.id) as post_count',
:group => 'blog_categories.id having post_count > 0',
:conditions => 'blog_posts.published = 1',
}
Problem I'm having is that the :select part of this seems to be getting completely ignored by rails, so the count field doesn't get put into the query, and I end up with the error "Unknown column 'post_count' in 'having clause'"
I don't know why it doesn't recognize post_count but it should work with:
:group => 'blog_categories.id having count(blog_posts.id) > 0',
Say I have a model Taggable has_many tags, how may I find all taggables by their associated tag's taggable_id field?
Taggable.find(:all, :joins => :tags, :conditions => {:tags => {:taggable_id => [1,2,3]}})
results in this:
SELECT `taggables`.* FROM `taggables` INNER JOIN `tags` ON tags.taggable_id = taggables.id WHERE (`tag`.`taggable_id` IN (1,2,3))
The syntax is incredible but does not fit my needs in that the resulting sql returns any taggable that has any, some or all of the tags.
How can I find taggables with related tags of field taggable_id valued 1, 2 and 3?
Thanks for any advice. :)
UPDATE:
I have found a solution which I'll post for others, should they end up here. Also though for the attention of others whose suggestions for improvement I'd happily receive. :)
Taggable.find(:all, :joins => :tags, :select => "taggables.*, tags.count tag_count", :conditions => {:tags => {:taggable_id => array_of_tag_ids}}, :group => "taggables.id having tag_count = #{array_of_tag_ids.count}"))
Your question is a bit confusing ('tags' seemed to be used quite a bit :)), but I think you want the same thing I needed here:
Taggable.find([1,2,3], :include => :tags).tags.map { |t| t.taggables }
or (if you want the results to be unique, which you probably do):
Taggable.find([1,2,3], :include => :tags).tags.map { |t| t.taggables }.flatten.uniq
This gets at all the taggables that have the same tags as the taggables that have ids 1,2, and 3. Is that what you wanted?