0

So I'm struggling a little on how to structure some data in Rails 3.

I have a table called Tasks and a table called Todos. A Todo can have up to 4 tasks, that are organized into the table like so:

Todos table:

  • id
  • todoname
  • slot1
  • slot2
  • slot3
  • slot4

Tasks table:

  • id
  • name
  • description
  • button_text
  • button_url

In the Todos table, slot1 through slot4 would have the ID of the associated task. I'm unsure how to structure the associations in the models.

Simple old has_many :tasks and belongs_to :todos don't quite work.

Any ideas?

2 Answers 2

2

I would do it this way

class Todo < ActiveRecord::Base
  has_many :tasks, :limit => 4
end

class Task < ActiveRecord::Base
  belongs_to :todo
end


resources :todos do
  resources :tasks
end

To get the 4 tasks for a @todo, you would do @todo.tasks

0
class Todo < ActiveRecord::Base
  has_many :tasks through :slots, :limit=>4
end
class Slot < ActiveRecord::Base
  belongs_to :todos
  belongs_to :tasks
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.