Call recent posts on the layout - ruby-on-rails-3

Imagine i have a blog, and i want a footer or sidebar displaying my 3 most recent posts at any given time.
What is the best way to do this?
I can call #recent_posts in every single controller to have them ready for the layout but this doesn't seem like the best way...at all...
#recent_posts = Posts.all(:limit => 3)
I've been fiddling around with partials, but they do need an instance variable carrying the #recent_posts.

There may be two parts to your concern: 1) performance, and 2) effort required. Both are easily addressed.
As Andrei S notes in his answer, the convenience/effort issue is mitigated by using a before_filter that calls the method that does the work from the ApplicationController class.
The performance issue is only slightly more work. Instead of the method being
def most_recent_posts
Posts.order(created_at DESC).limit(3)
end
instead do this
def most_recent_posts
#most_recent_posts ||= Posts.order(created_at DESC).limit(3)
end
which checks the instance variable for nil; if nil, it does the query and assigns the result to the instance variable.
You'll also need a way to update when a new post is added, so perhaps something like
def clear_most_recent_posts!
#most_recent_posts = nil
end
and then just call clear_most_recent_posts! from the method(s) that modify the table. The before_filter will do its work only when needed.
I am sure some more eloquent rubyist has a nicer way of doing this, but this is an idea.

You could put the part where you have your posts in a partial and use it in the general layout of your app.
To load them all in every controller you could do a before_filter in your ApplicationController in which you set your instance variable, which will be available in your partial that gets rendered in the layout
This way you only get to do it once, and it will get done everywhere (of course you could set conditions on the filter and the layout to load them when you need, that's if you don't really need them on every page)

Related

Load object from model on every page?

I'm looking to load a single (chosen randomly) object from a single table in my database on every page of my rails app.
For example, a quotes table which has several quotes in the table, and I just want one on every page load.
What's the best way to accomplish this? Obviously copy & pasting the query into each controller isn't the right way to go about this.
I wouldn't use before_filter for this, there is no need to access database on redirecting actions and other "not rendered" actions. Instead I would use helper_function, and I would call it in the layout, as you need to position it anyways.
def random_quote
#quote ||= "Select random quote here"
end
helper_method :random_quote
Your method of selecting a quote is up to you. You just need to access random_quote as a normal helper method in the layout. This only access one quote per action, only if the layout is rendered.
This kind of stuff typically goes into a before_filter in the ApplicationController :
before_filter :get_random_quote
#This code is executed before every action of your app
def get_random_quote
random_id = ...#Generate a random ID ...
#random_quote = Quote.find(random_id)
end
Then in your views, just refer to #random_quote. Done!
Edit : on second thought, Matzi solution seems smarter. The request will only get called when you actually output something. Nothing's wasted.
Assuming PostgreSQL:
before_filter :get_quote
def get_quote
#quote = Quote.order('RANDOM()').limit(1)
end

Make URL be title of post

Currently my URL's appear as www.website.com/entries/1, I'd like to make them appear as www.website.com/title-of-entry. I've been messing around with routes and have been able to get the entry title to display in the URL, but Rails is unable to find the entry without supplying an ID. If I send the ID along with the parameters, the URL appears as www.website.com/title-of-entry?=1. Is there anyway I can pass the ID without having it appear in the URL as a parameter? Thanks!
Like most things, there's a gem for this.
FriendlyID.
Installation is easy and you'll be up and running in minutes. Give it a whirl.
Ususally you'll want to to save this part in the database title-of-entry (call the field slug or something`). Your model could look something like this:
class Entry < ActiveRecord::Base
before_validation :set_slug
def set_slug
self.slug = self.title.parameterize
end
def to_param
self.slug
end
end
Now your generated routes look like this: /entries/title-of-entry
To find the corresponding entries you'll have to change your controller:
# instad of this
#entry = Entry.find(params[:id]
# use this
#entry = Entry.find_by_slug(params[:id])
Update
A few things to bear in mind:
You'll have to make sure that slug is unique, otherwise Entry.find_by_slug(params[:id]) will always return the first entry with this slug it encounters.
Entry.find_by_slug(params[:id]) will not raise a ActiveRecord::RecordNotFound exception, but instead just return nil. Consider using Entry.find_by_slug!(params[:id]).
If you really want your routes to look like this /title-of-entry, you'll probably run into problems later on. The router might get you unexpected results if a entry slug looks the same as another controller's name.

Rails-3.1 nested routes creating same action and controller, basic lack of knowledge

Doing the following routes configuration:
resources :cadeiras do
resources :professores
end
resources :cadeiras do
resources :fichas
end
resources :fichas do
resources :exercicios
end
will generate me 2 different links to the same controller and action, running rake routes ill get something like:
fichas GET /fichas(.:format) {:action=>"index", :controller=>"fichas"}
cadeira_fichas GET /cadeiras/:cadeira_id/fichas(.:format) {:action=>"index", :controller=>"fichas"}
The first action will reference all the 'fichas' while the second on is referencing only 'fichas' from 'cadeiras' how is it possible to distinguish the two actions?
I would like to avoid three level nesting problems as described here :http://weblog.jamisbuck.org/2007/2/5/nesting-resources
Thank you for your time
If I understand your question correctly, the answer is "you don't distinguish them" :
The exact same action is executed from the controller, rendering the exact same view. The difference is the collection of 'fichas' that get sent to the view:
- in the first case, all fichas are available in the view
- in the second case, only the 'fichas' related to the 'cadeira' are available in the view (e.g. /cadeira/1/fichas will display only the 'fichas' related to the 'cadeira' with id 1)
To determine which records to show (e.g.) in an index view, you can do something like this:
unless cadeira_id = params[:cadeira_id]
#fichas = Ficha.all
else
#fichas = Cadeira.find(cadeira_id).fichas
end
The rest is up to the view: it should render fichas the same way, you just chose which records are actually made available to it.

Rails 3 - constructor action in controller?

I need to store a value from the database in a variable, and I need to access this variable from all the actions in my controller.
For example, I have the controller home which has 3 actions:
about
contact
blog
I tried to store the value to a variable in application_controller.rb, but this did not work.
How can I do this?
This is about persistence. To get around the fact website are stateless you persist data by storing it in a cookie. Rails gives you a convenient way of doing this called a session hash.
You can also use the flash hash (This is also a session hash)
At the point where you get the value that you wish to store simply call
session[:some_variable] = some_variable
Then when you want to retrieve that variable just call
some_variable = session[:some_variable]
Replace some_variable with a something that makes sense to you
If you want to do this for all controller actions then a before_filter can be useful
Read here about before_filters http://apidock.com/rails/ActionController/Filters/ClassMethods/before_filter
Do not store large objects or arrays of objects in the session. If you need to keep a reference to an active_record object then store just the ID of the record then you can retrieve that record with a find when you need it
If you want to access to a specific variable in all your controller, just do the following.
Into the application controller :
before_filter :set_my_variables
private
def set_my_variables
#variable = MyModel.find(YOUR_CONDITION)
end
And for instance, in your home controller, you will be able to access to #variable.
Hope this helps.

Mongoid: Changing the order of documents in an embeds_many relation

I have a mongoid document which embeds other documents with a relation like
this:
embeds_many :blocks
Creating new blocks works fine, but I cannot manage to change the
order of existing embedded documents. For example I have three
embedded blocks and I want to move the last one to the first
position.What's the correct way to do that?
I had to deal with this with mongoid's recursively_embeds_many feature, but it's essentially the same. There's nothing wrong as far as I can tell with literally rewriting the document. Write a model method to do something like:
def reverse_blocks
reversed_blocks = blocks.to_a.reverse
blocks.clear
reversed_blocks.each do |b|
blocks.create b.attributes
end
save
end
That's not great code above, but it gives you an idea of how to do what you want to do. I'm not thrilled with having to go through that just to reorder stuff in an array, but there it is.
I think, that really correct way is make in your embedded docs field "weight" and query them with asc(:weight) or desc(:weight). You don't rely on the order of persisted non-embedded docs, so you shouldn't in embedded.
But if you urgently need to make this, your embedded docs in mongoid are just array, so you can do such way:
doc.embedded_docs = [doc.embedded_docs.last] + doc.embedded_docs[0..-2]