- I have a table (posts) with a column called ‘tag’ I want to be able to call a particular set of tagged posts (Sports, Arts & Entertainment, etc) so that when a user clicks on the corresponding links, they only get posts from that category.
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!
If your
tagcolumn is just text, then it would be as simple as this:Using
scopeis covered in this Rails Guide.As for your other question, as long as your
poststable has acreated_atcolumn (provided with thetimestampsmigration helper, then you can order them reverse-chronologically (newest first) like so:orderis 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 useSomeModel.all,SomeModel.first, etc.