0

Let's say I got tasks and lists with an has_and_belongs_to_many relationship:

class Task < ActiveRecord::Base
  attr_accessible :content, :due_date
  has_and_belongs_to_many :lists
end


class List < ActiveRecord::Base
  attr_accessible :title, :user_id, :space_free_title
  has_and_belongs_to_many :tasks
end

Also I got the related model/table since a task can be on many lists:

class ListsTasks < ActiveRecord::Base
  attr_accessible :list_id, :task_id
end

Now I know how to get all ListsTasks by the list_id:

ListsTasks.find_all_by_list_id(1)


But how do I get the contents of a task based on ListsTasks?

4

1 回答 1

1

Your ListsTasks model is unnecessary and is not being used by the two associations in your List and Task models.

Are you looking for something like List.find(1).tasks to get the tasks on that list?

于 2013-01-19T09:53:07.290 回答