Basically I currently have this system where there are categories, and
tutorials; obviously categories have many tutorials, and every
tutorial must belong_to a category.
The tutorials are then displayed in order of there id (auto-
incrementing) in their categories show method.
The thing is, I want to create a link that goes from one tutorial
(with an id of 10 for example) to the next tutorial in that category
(which might not have an id of 11, as I create different tutorials at
different times, therefore creating different ids for two tutorials
seemingly next to eachother in order).
Obviously I know I will need something like this:
<%= link_to ‘Next lesson…’, %>
but I just don’t know how I can manage it…
By the way, every tutorial has a: category_id to determain which
category they are in (this is an integer).
By the way, every tutorial has a: category_id to determain which
category they are in (this is an integer).
If I were you, i’d create a position column on tutorials, which
determines the ordering of tutorials for a category (ordering by id
might seem the simplest but stops you from ever reordering tutorials.
Given a tutorial, the next one is the first one in the same category
whose position is greater than the current ones (you could write a
next_tutorial method on tutorial). There’s a plugin called
acts_as_list that you might find helpful maintaining the position
column. If you’re hell bent on ordering by id then it’s as if id is
your position column
whose position is greater than the current ones (you could write a
next_tutorial method on tutorial). There’s a plugin called
acts_as_list that you might find helpful maintaining the position
column. If you’re hell bent on ordering by id then it’s as if id is
your position column
Fred
I agree with Fred as it allows you to easily re-order the tutorials if
you want to, but if you don’t… then…
Ok, so I’ve created my position thing for every tutorial. However now
I need to know how I can setup render :partial to order the tutorials
using the position, and then if two (or more) tutorials have the same
position, then go back and check there id, and order the ones with the
same position by id.
This is how I’m currently displaying the tutorials:
Ok, So it seems that this whole tutorial ordering thing seems to be
the best way to go (thanks to philip anyway for giving me the code to
do it with ids if I wanted to);
The problem is I’m really a complete noob in RoR. I know how to create
migrations, however creating a “position” variable i which they were
independent to the category the tutorials were in; I have absolutely
no idea how to do that. And you talk about writing methods, I also do
not know where these methods would be created (inside a controller
perhaps?).
Any help with this task would be kindly appriciated,
So I change it to this: <%= render :partial => @category.tutorials.ordered_by_position %>
and I get the error: undefined method `ordered_by_position’
I’m guessing that’s because I have to create a method that orders them
by position. However I’m a noob in ruby on rails, where do I create
the method, and how do I make it order them by position?
I’d probably create a named_scope on Tutorial which ordered things by
position. Since named_scopes can be chained with each other and with
an association you could do @category.tutorials.ordered_by_position
Thanks for your help, but I’m not sure where classes go.
I’ve tried copying this code into the tutorials_controller:
def self.ordered_by_position
find :all, :order => ‘position’
end
But it still has this error: undefined method `ordered_by_position’
I assume this is because I have either put this code in the wrong
place; of need to put it in a different place WITH this bit: class
Tutorial < AR::Base (and of course the extra end)…
using “self” in the method name makes it a class method; so accessible
with “Tutorial.next_lesson”. But you’d have to pass in the tutorial_id
and category_id.
Seriously - someone much earlier suggested using acts_as_list. Do. It
deals with all of this for you…
So I change it to this: <%= render :partial => @category.tutorials.ordered_by_position %>
and I get the error: undefined method `ordered_by_position’
I’m guessing that’s because I have to create a method that orders them
by position. However I’m a noob in ruby on rails, where do I create
the method, and how do I make it order them by position?
Well a trick here is that if you make a class method on Tutorial then
you can call it on a tutorials association and will work just fine,
and any finds and so on are scoped to the association. So you could
write
class Tutorial < AR::Base
def self.ordered_by_position
find :all, :order => ‘position’
end
end
A better choice for this sort of thing is named_scope. They are more
flexible and more reusable. Have a look at the docs for those.
When I change it to “Tutorial.next_lesson” it then says: undefined
method `nil_class_path’
Is this because I have to “pass in the tutorial_id and category_id” or
is it a different problem?
As for acts_as_list, I would prefer not to use it for now and just
keep with using position; However it is something I will be looking at
for future projects…
As for acts_as_list, I would prefer not to use it for now and just
keep with using position; However it is something I will be looking at
for future projects…
Why?! acts_as_list uses the position column itself. You can scope it
(so you can have a list per category, or whatever) and it gives you
the next/previous item methods:
When I change it to “Tutorial.next_lesson” it then says: undefined
method `nil_class_path’
Is this because I have to “pass in the tutorial_id and category_id” or
is it a different problem?
You’re probably getting nil_class_path because you are returning nil
from somewhere.
You definitely need to pass in the values of tutorial_id - that class
method isn’t going to be stealing instance variables out of the
controller or anything like that.
Personally I’d have next_lesson be an instance method of tutorial