Reputation: 45513
Here is what I have tried to implement so far: (in my posts model)
def self.seperate_by_tag
@tag = find(:all, :tag => ??)
end
I'm assuming I need to input some sort of parameter into the :tag portion in order to access a set of posts with a particular tag, however, I'm not sure how to go about doing so.
I also am having trouble with displaying posts in the order they are posted (i'd like them to be at the top).
def self.find_posts
find(:all, :order => ??)
end
Any help would be much appreciated!
Upvotes: 0
Views: 138
Reputation: 106087
If your tag
column is just text, then it would be as simple as this:
class Post < ActiveRecord::Base
scope :by_tag, lambda {|tag_name| where :tag => tag_name }
end
# Usage:
Post.by_tag('Sports').all # => collection of posts
Using scope
is covered in this Rails Guide.
As for your other question, as long as your posts
table has a created_at
column (provided with the timestamps
migration helper, then you can order them reverse-chronologically (newest first) like so:
Post.order('created_at DESC').all
order
is covered in this guide.
P.S. As you might have gathered from my code, the SomeModel.find :all, ...
syntax is officially deprecated. You should instead use SomeModel.all
, SomeModel.first
, etc.
Upvotes: 2