在Ruby on Rails中为RESTful API应用程序扩展模型

44u64gxh  于 5个月前  发布在  Ruby
关注(0)|答案(2)|浏览(41)

作为一个项目的一部分,我们有一个UML图要实现。现在我在OOP的一部分,但我不知道如何在Ruby中实现它。每个任务都是一个家务或家庭作业,因为我理解它必须是多表继承(MTI)。我不知道如何实现用户表->家务/hw的表之间的关系,也任务->家务/hw的表。此外,这将是很好的知道如何实现CRUD行动(创建\更新\销毁)希望有人可以帮助。谢谢。这是图表:Diagram

4uqofj5v

4uqofj5v1#

即使您的UML模式在Rails中实现时违反了依赖倒置原则,并且违反了2NF
但你可以这样做:
您需要创建两个表peopletasks
您可以通过添加以下迁移来实现这一点:

class CreatePeople < ActiveRecord::Migration[7.0]
  def change
    create_table :people do |t|
      t.string :name
      t.string :email
      t.string :fav_prog_lang
      t.timestamps
    end
  end
end

个字符
然后,你需要创建这些模型:

# app/models/person.rb

class Person < ApplicationRecord
  has_many :tasks, foreign_key: :owner_id
end
# app/models/task.rb

class Task < ApplicationRecord
  belongs_to :owner, class_name: 'Person', foreign_key: :owner_id

  enum status: { active: 0, done: 1 }
end
# app/models/chore.rb

class Chore < Task
  enum size: { small: 0, medium: 1, large: 2 }
end
# app/models/homework.rb

class Homework < Task
  enum size: { small: 0, medium: 1, large: 2 }, _prefix: true
end

最后,你可以创建你的任务:

person = Person.create(name: 'John Doe', email: '[email protected]', fav_prog_lang: 'Ruby')

Homework.create(owner: person, course: 'Some course', due_date: 10.days.since, details: 'Some details')

Chore.create(owner: person, size: :medium, description: 'Some description')

person.tasks


如果您需要能够知道什么任务是chore,什么任务是homework,您需要将type字段添加到tasks表中,以便能够确定子任务

更新:

为了避免违反依赖倒置原则,你可以做下面的事情:
TaskChoreHomework分隔到不同的表中,因为ChoreHomework具有非常不同的字段

class CreateTasks < ActiveRecord::Migration[7.0]
  def change
    create_table :tasks do |t|
      t.references :owner, null: false, foreign_key: { to_table: :people }
      t.integer :status, default: 0

      t.timestamps
    end
  end
end
class CreateChores < ActiveRecord::Migration[7.0]
  def change
    create_table :chores do |t|
      t.references :task, null: false, foreign_key: true
      t.text :description
      t.integer :size, default: 0
      t.timestamps
    end
  end
end
class CreateHomeworks < ActiveRecord::Migration[7.0]
  def change
    create_table :homeworks do |t|
      t.references :task, null: false, foreign_key: true
      t.string :course
      t.datetime :due_date
      t.string :details
      t.timestamps
    end
  end
end

还有你的模特:

class Person < ApplicationRecord
  has_many :tasks, foreign_key: :owner_id
  has_many :chores, through: :tasks
  has_many :homeworks, through: :tasks
end
class Task < ApplicationRecord
  belongs_to :owner, class_name: 'Person', foreign_key: :owner_id
  has_many :chores
  has_many :homeworks

  enum status: { active: 0, done: 1 }
  
  accepts_nested_attributes_for :chores, :homeworks
end
class Chore < ApplicationRecord
  belongs_to :task
  enum size: { small: 0, medium: 1, large: 2 }
end
class Homework < ApplicationRecord
  belongs_to :task
end

你可以像这样操作这些模型:

person = Person.create(name: 'John Doe', email: '[email protected]', fav_prog_lang: 'Ruby')

person.tasks.create(
  status: :done, chores_attrubutes: [{ size: :medium, description: 'Some description' }]
)

person.tasks.create(homeworks_attrubutes: [{ course: 'Some course', due_date: 10.days.since, details: 'Some details' }])

person.chores

person.homeworks

# and you can have also all tasks

person.tasks

注:但是,如果您需要添加更多不同的任务,这种方法并不适合。因为您将始终拥有新的任务类型,您将不得不添加新的表。这只适用于您当前的示例。

68bkxrlz

68bkxrlz2#

models/person.rb
class Person < ApplicationRecord
  has_many :tasks, as: :owner, class_name: "::Task"
end

models/task.rb
class Task < ApplicationRecord
  belongs_to :owner, polymorphic: true

  enum status: {
    active: 0,
    done: 1
  }

  scope :chores, -> { where(type: "Tasks::Chores")}
  scope :homeworks, -> { where(type: "Tasks::Homework")}
end

models/tasks/chores.rb
class Tasks::Chores < Task
  enum size: {
    small: 0,
    medium: 1,
    large: 2
  }
end

models/tasks/homework.rb
class Tasks::Homework
end

字符串
我没有试过这个代码,但它应该是正确的。
Task有一个多态的owner,所以它可以是一个Person,或任何其他模型,这将增加2列owner_typeowner_id
一个任务也有一个type(STI),它可以是Tasks::ChoresTasks::Homework(为了清楚起见,我给它命名)。
使用Task模型上的作用域,您应该能够调用person.tasks.choresperson.tasks.homeworks

相关问题