ruby on rails has_many association

时间:2014-01-07 09:13:43

标签: ruby-on-rails associations

我正在研究todo list app.so基本上我创建了表,一个是     子任务,项目和连接表是分配。

class Project < ActiveRecord::Base
validates :name,presence: :true
validates :description,presence: :true
has_many :subtasks,through: :assigns

end
class Subtask < ActiveRecord::Base
has_many :projects,through: :assigns

end
class Assign < ActiveRecord::Base
belongs_to :project
belongs_to :subtask
end

使用这个应用程序我基本上想先创建一个项目。然后该项目将为     创造三个任务,即开展生产,生产,测试以及完成这些任务     是子任务,这是最重要的。如果对于像devlopement这样的任务,我有     todo的“正在处理jquery错误”,“正在处理php”,这样的todo与任务相关联。

So,finally now my question is are my associations correct.coz my project should 
definitely have has_many with subtasks,but i think that every subtask belongs
to individual projects and diff projects will have diff subtasks.  

Please suggest me any changes if i am goin wrong sumwhere or if my associations 
are incorrect.

i have added the associations now,pls hav a look at it

And this is how the database looks
projects
id    name     description     created_at      updated_at

subtasks
id    name     description     created_at      updated_at

assigns
id    project_id    task_id  taskname created_at   updated_at

3 个答案:

答案 0 :(得分:0)

这里根据这行代码

 assigns
id    project_id    task_id  taskname created_at   updated_at
看到你制造了并且属于许多协会但是这是不可能的,因为在拥有并且属于许多协会你不能添加taskname created_at updated_at id cols有一个限制has并且属于很多所以如果你想要使用创建has_many through association

的字段

答案 1 :(得分:0)

从你写的内容来看,我会看看这个:


<强>模型

#app/models/project.rb
Class Project < ActiveRecord::Base
    has_many :tasks
    has_many :subtasks, through: :tasks

    after_create :add_tasks

    private
    def add_tasks
        tasks.create(name: "Production")
        tasks.create(name: "Development")
        tasks.create(name: "Test")
    end 
end

#app/models/task.rb
Class Task < ActiveRecord::Base
    belongs_to :project
    belongs_to :subtask
end

#app/models/subtask.rb
Class SubTask < ActiveRecord::Base
    has_many :tasks
    has_many :projects, through: :tasks
end

每次taskscreate时,这会创建一组Project。这意味着您可以使用subtasks关联(使用has_many :through作为连接模型)为每项任务分配Task


<强>架构

projects
id    name  description     created_at      updated_at

tasks
id    project_id    subtask_id  created_at updated_at

subtasks
id    name  description  created_at   updated_at

这可以通过以某种方式使Tasks不变来大量减少干扰。我不得不考虑如何做到这一点(可能使用像ConfigSpartan这样的宝石使用系统范围的配置变量


<强> has_many :through

我认为您对如何访问正确的数据感到困惑。我会尽力解释它是如何为你服务的!

如果您想拥有“子”模型,则必须使用has_many :through关系才能创建many_to_many关联。这意味着您可以像这样调用您的数据:

#app/models/project.rb
Class Project < ActiveRecord::Base
    scope :production, -> { joins(:tasks).where("task.name = Production") }
    scope :development, -> { joins(:tasks).where("task.name = Development") } 
    scope :test, -> { joins(:tasks).where("task.name = Test") } 
end

#app/controllers/projects_controller.rb
def show
     @project_production = Project.production.subtasks
     @project_development = Project.development.subtasks
     @project_test = Project.test.subtasks
end

#app/views/projects/show.html.erb
<h2>Production</h2>
<% @project_production.each do |subtask| %>
    <%= subtask.name %>
<% end %>

<h2>Development</h2>
<% @project_development.each do |subtask| %>
    <%= subtask.name %>
<% end %>

<h2>Test</h2>
<% @project_test.each do |subtask| %>
    <%= subtask.name %>
<% end %>

答案 2 :(得分:0)

你在这里错过了一个至关重要的has_many协会。 您需要在ProjectSubTask模型中再添加一行, has_many :assigns

因此,您的Project模型应该是

Class Project < ActiveRecord::Base
    has_many :assigns
    has_many :subtasks, through: :assigns
end

同样适用于SubTask模型