0

I'm trying to use the gem 'ranked-model' in Rails 3.2.

This is the model:

class Costproject < ActiveRecord::Base
  include RankedModel
  scope :active, where("coststatus_id IN (?)", [2, 3, 4, 5, 6] ).where("project_year = ?", Time.now.year + 1)
  ranks :row_order,
        :scope => :active

I ran this in the Rails console:

Costproject.rank(:row_order).all

After, the column row_order is blank in all the records.

What am I doing wrong?

Thanks for the help!

UPDATE1

I believe now that Costproject.rank(:row_order).all is used to list the projects. I thought it was a command to seed the numbers in the list. Maybe I don't have to do that?

Reddirt
  • 5,913
  • 9
  • 48
  • 126

1 Answers1

0

Per our conversation via comments. The problem looks like you need to seed your database before you can use the rank command.

So you can seed your dev database a few ways, via rails console, using your app or modifying your seeds.rb file.

I'm going to suggest you use your seeds.rb file because it's idempotent ( http://en.wikipedia.org/wiki/Idempotence )

So open db/seeds.rb or create one if you don't have one.

Create a few records:

Costproject.create(rank_order: 3, coststatus_id: 2, project_year: 2016)
Costproject.create(rank_order: 2, coststatus_id: 2, project_year: 2016)
Costproject.create(rank_order: 1, coststatus_id: 2, project_year: 2016)

This will insert 3 records into costprojects table. You MUST have the coststatus_id and project_year set to those values per your scope. These won't work:

Costproject.create(rank_order: 10)
Costproject.create(rank_order: 20, coststatus_id: 10)
Costproject.create(rank_order: 30, coststatus_id: 2, project_year: 2013)

then via terminal you can run:

bundle exec rake db:seed

This rake task will run your seed file as if you were manually typing it in your rails console.

Now you should be able to do Costproject.rank(:row_order).all. The first model should have an id of 3, assuming you ran your seed file on an empty database.

For more info:

What is the best way to seed a database in Rails?

http://railscasts.com/episodes/179-seed-data

http://www.jasonwieringa.com/rake_rails_and_databases/

Community
  • 1
  • 1
devkaoru
  • 1,142
  • 9
  • 7