0

I am just starting out with Ruby. I am making a little app in Sinatra and I am using Datamapper with a sqlite3 db.

Below are the three models I have that I am creating.

class Team
  include DataMapper::Resource
  property :id, Serial
  property :name, String, :required => true
  property :created_at, DateTime
  property :updated_at, DateTime
end

class Poll
  include DataMapper::Resource
  property :id, Serial
  property :name, String, :required => true
  property :created_at, DateTime
  property :updated_at, DateTime
end

class Ranking
  include DataMapper::Resource
  property :year, Integer
  property :week, Integer
  property :ranking, Integer
  property :votes, Integer
  property :created_at, DateTime
  property :updated_at, DateTime

  belongs_to :team, :key => true
  belongs_to :poll, :key => true
end

What I want to be able to do is query the Ranking model for a certain poll, week and year.

The returning result should be all the rankings for that poll with the associated team to each ranking number.

So get the ranking and corresponding team for each ranking for say 2011 - Week 1 or 2011 - Week 7, etc...

I have been trying all day to figure out how to get this to work and I am not getting anywhere, so that is why I am now posting here asking for help.

Daniel
  • 22,521
  • 12
  • 107
  • 150
oneab
  • 1
  • 1

1 Answers1

0

Let me start by saying that I had never heard of the Datamapper gem. It's going to be easier for me to keep thinking with the Rails models and migrations, so I hope you don't mind me doing that.

I have some comments about the data model:

  • Unless I've misunderstood your model, I think all date-related fields should go in the polls table rather than the rankings table. The fields in the rankings table should be descriptive of the relationship between teams and polls.
  • You should use a single field for the poll date and then use something like the commercial method in your controller to retrieve polls within a certain week. (Source: http://apidock.com/ruby/Date/commercial/class)
  • You could also store additional data related to the polls if you wish, such as total number of votes. Those would also go into the polls table.

Models

app/models/team.rb

class Team < ActiveRecord::Base
    has_many :rankings
    has_many :polls, through: :rankings
    validates :name, :presence => true
end

app/models/poll.rb

class Poll < ActiveRecord::Base
    has_many :rankings
    has_many :teams, through: :rankings
    validates :name, :presence => true
end

app/models/ranking.rb

class Ranking < ActiveRecord::Base
    belongs_to :team
    belongs_to :poll
end

Migrations

db/migrate/create_teams.rb

class CreateTeams < ActiveRecord::Migration
    def change
        create_table :teams do |t|
            t.string :name
            t.timestamps
        end
    end
end

db/migrate/create_polls.rb

class CreatePolls < ActiveRecord::Migration
    def change
        create_table :polls do |t|
            t.string :name
            t.date :published_at
            t.timestamps
        end
    end
end

db/migrate/create_rankings.rb

class CreateRankings < ActiveRecord::Migration
    def change
        create_table :rankings do |t|
            t.integer :team_id
            t.integer :poll_id
            t.integer :votes
            t.integer :ranking
            t.timestamps
        end
    end
end

Please let me know if you managed to get it working since I didn't have time to set up a test application to check the relationships.

Marcelo De Polli
  • 24,917
  • 4
  • 35
  • 47