1

所以,我有一个模型,任务,它应该允许父母与孩子的关系。这些是模型定义的相关部分:

class Task < ActiveRecord::Base

  attr_accessible :relationships_attributes

  has_many :relationships, :foreign_key => "subtask_id", :dependent => :destroy
  has_many :parents, :through => :relationships, :source => "parent"

  has_many :reverse_relationships, :foreign_key => "parent_id", :class_name => "Relationship", :dependent => :destroy
  has_many :subtasks, :through => :reverse_relationships, :source => "subtask"

end

关系模型的定义如下:

class Relationship < ActiveRecord::Base

  attr_accessible :parent_id

  belongs_to :parent, :class_name => "Task"
  belongs_to :subtask, :class_name => "Task"

  validates :parent_id, presence: true
  validates :subtask_id, presence: true

end

我想做的是通过使用包含复选框的表单来修改基础“关系”数据库表中存在的条目,该复选框允许为特定任务选择/删除父任务 ID。这是我的 edit.html.erb 文件中用于 check_box_tag 的代码:

<p>
  <br>
  <%= f.label :parent_tasks %>
  <% for task in Task.find(:all) %>
  <div>
    <%= check_box_tag "subtask[parent_ids][]", task.id, @task.parent_ids.include?(task.id)%>
    <%= task.id %>
  </div>
  <% end %>
</p>

当按下表单的保存按钮时,不会产生错误,但不会保存在复选框中所做的修改。日志显示这些条目(它不会尝试从关系表中删除/插入):

Started PUT "/dev/tasks/25" for 127.0.0.1 at 2012-04-30 14:59:13 -0400
Processing by Dev::TasksController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"+ShrgdwgD7G1l9iDEuCsZ84d9XCwXh01p3BwJARJScE=", "task"=>{"name"=>"task bla", "description"=>"", "project_id"=>"", "task_status_id"=>"1"}, "subtask"=>{"parent_ids"=>["19", "22"]}, "commit"=>"Save changes", "id"=>"25"}
  User Load (0.8ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'z7IrWT7R7u7i6nhW26J2Og' LIMIT 1
  Task Load (0.3ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = $1 LIMIT 1  [["id", "25"]]
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
Redirected to http://localhost:3000/dev/tasks
Completed 302 Found in 8ms (ActiveRecord: 1.3ms)

我之前已经为两个不同的模型 User 和 Team 实现了我想要实现的功能,这两个模型有一个联合模型 Teams_users。这是成功保存更改到 teams_users 数据库表的操作的日志:

Started PUT "/teams/1" for 127.0.0.1 at 2012-04-30 14:46:11 -0400
Processing by TeamsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"+ShrgdwgD7G1l9iDEuCsZ84d9XCwXh01p3BwJARJScE=", "team"=>{"name"=>"My team", "user_ids"=>["3", "8", "9", "100", "101"]}, "commit"=>"Save", "id"=>"1"}
  Team Load (0.3ms)  SELECT "teams".* FROM "teams" WHERE "teams"."id" = $1 LIMIT 1  [["id", "1"]]
   (0.1ms)  BEGIN
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" IN (3, 8, 9, 100, 101)
  User Load (0.4ms)  SELECT "users".* FROM "users" INNER JOIN "teams_users" ON "users"."id" = "teams_users"."user_id" WHERE "teams_users"."team_id" = 1
   (0.2ms)  DELETE FROM "teams_users" WHERE "teams_users"."team_id" = 1 AND "teams_users"."user_id" IN (2, 4)
   (38.3ms)  INSERT INTO "teams_users" ("team_id", "user_id") VALUES (1, 3) RETURNING "id"
  Team Exists (0.4ms)  SELECT 1 FROM "teams" WHERE (LOWER("teams"."name") = LOWER('My team') AND "teams"."id" != 1) LIMIT 1
   (39.9ms)  COMMIT
Redirected to http://localhost:3000/teams/1
Completed 302 Found in 88ms (ActiveRecord: 81.0ms)

我希望使我当前的代码工作所需的修改很小,并且有人能够将我指向它们或我尚未遇到的一些相关帖子:)。

4

0 回答 0