简体   繁体   English

如何在ruby中为用户和任务模型之间的has_many,belong_to关联创建迁移脚本

[英]How to create migration script for has_many, belong_to association between user and task model in ruby

I have two model User and Task. 我有两个模型用户和任务。 Task would have one owner and other as workers. 任务将有一个所有者,另一个作为工人。

class Task < ApplicationRecord
    belongs_to :user
    has_many :user
end



class User < ApplicationRecord 
    has_many :user
end

how write its migration script. 如何编写其迁移脚本。 i dont know much about ruby so please correct if i am wrong. 我对红宝石不太了解,所以如果我错了,请纠正。

You have create a separate table for has_many through association to join multiple users to multiple tasks 您已经通过关联为has_many创建了一个单独的表,以将多个用户加入多个任务

class User < ApplicationRecord

    # association to define tasked owned by users
    has_many :tasks, :class_name => 'Tasks', :foreign_key => 'owner_id'

    # second association to related worker tasks for each user
    has_many :tasks_workers
    has_many :workerfortasks, through: :tasks_workers,  :source => :tasks
end

class Task < ApplicationRecord

    belongs_to :owner , :class_name => 'User', :foreign_key => 'owner_id'       
    has_many :users_tasks
    has_many :workers, through: :tasks_workers,  :source => :users
end

class TasksWorkers < ApplicationRecord
    belongs_to :user
    belongs_to :task
end

Then, you load the tasks for each user using eager_load or includes joins like following or vice versa 然后,您使用eager_load加载每个用户的任务,或者包括以下或类似的联接

  user = User.find(user_id).eager_loads(:tasks)

To generate the migration scripts, run the following terminal commands from the root of the project folder. 要生成迁移脚本,请从项目文件夹的根目录运行以下终端命令。 Note - You can add additional properties to the tables according to your usecase. 注意-您可以根据用例向表中添加其他属性。

#Generate task migration script
$ rails g model tasks owner_id:integer name:string

#Generate user migration script
$ rails g model users name:string email:string

#Generate tasksworkers migration script
$ rails g model tasks_workers task_id:integer user_id:integer

The above three rails commands will generate three migration script in your project_root/db folder. 上面的三个rails命令将在project_root / db文件夹中生成三个迁移脚本。

You can also refer to the same question posted here . 您也可以参考此处发布的相同问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建和保存具有has_many / belong_to关联的模型 - How to create and save an model that has a has_many/ belong_to association 在Ruby on Rails中,模型“ has_many”和“ belong_to”如何使用除主ID之外的其他字段? - In Ruby on Rails, how can a model “has_many” and “belong_to” using a different field other than primary ID? has_many,belong_to和两者之间的区别 - Difference between has_many, belong_to, and both 我怎样才能使用户具有has_many个对象,但每个对象都可以属于许多用户? - How can I make a user has_many objects but each object can belong_to MANY users? Rails has_many / belong_to协会 - Rails has_many/belong_to Associations 在rails中的has_many中的关联 - Associations in has_many , belong_to in rails 如何使用联接表在Rails中创建has_many和belong_to一个关联? - How to create has_many and belong_to one associations in Rails using a join table? has_many和belong_to关联出现问题,找不到方法 - Issue with has_many and belong_to association, can't find method 数据库关联,类has_many和belonge_to可以属于同一对象吗? - Database Association, Can a class has_many and belong_to the same object? 要让has_many归属_起作用,需要迁移中的外键吗? - foreign key in migration is needed to let has_many belong_to work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM