如何在Ruby on Rails中实现请愿与用户模型之间的关系?

时间:2022-09-02 08:09:39

I am wondering what is the best way to model a relationship between petitions and users in Ruby on Rails. Here are the constraints

我想知道在Ruby on Rails中建立请愿与用户之间关系的最佳方法是什么。这是约束

-a user own a petition

- 用户拥有请愿书

-a user create a petition

- 用户创建请愿书

-one user can be the author of a petition

- 一个用户可以是请愿书的作者

-all the users can sign a petition

- 所有用户都可以签署请愿书

Is this a polymorphism case?

这是多态性案例吗?

2 个解决方案

#1


2  

The petition can simply belong to the user that created it using the normal has_many and belongs_to relationship definitions. Then use a HABTM table to track the signers.

请愿书可以简单地属于使用普通has_many和belongs_to关系定义创建它的用户。然后使用HABTM表来跟踪签名者。

For the User class I would define...

对于User类,我会定义...

  has_many :created_petitions, :class_name => 'Petition', :foreign_key => 'creator_id'
  has_and_belongs_to_many :signed_petitions, :class_name => 'Petition', :association_foreign_key => 'signer_id'    

And for the Petition class I would define...

对于请愿课我会定义......

   belongs_to :creator, :class_name => 'User'
   has_and_belongs_to_many :signers, :class_name => 'User', :foreign_key => 'signer_id'

Then the migration would be...

然后迁移将是......

create_table :petitions_signers, :id => false do |t|
  t.integer :petition_id
  t.integer :signer_id
end

add_column :petitions, :creator_id, :integer

#2


1  

Polymorphism is when you're dealing with relationships between one entity and a number of unrelated entities. If it's between one entity and a number of similar entities, you may want to use STI instead.

多态性是指处理一个实体与许多不相关实体之间的关系。如果它在一个实体和许多类似实体之间,您可能希望使用STI。

Since you're talking about a simple User to Petition relationship, there's no reason to worry about polymorphism. What you need is a User model, a Petition model, and a UserPetition join model that records information about what their relationship to the petition is, such as creator or signer.

既然你在谈论一个简单的用户与请愿关系,那么没有理由担心多态性。您需要的是用户模型,请愿模型和UserPetition连接模型,它记录有关他们与请愿书的关系的信息,例如创建者或签名者。

#1


2  

The petition can simply belong to the user that created it using the normal has_many and belongs_to relationship definitions. Then use a HABTM table to track the signers.

请愿书可以简单地属于使用普通has_many和belongs_to关系定义创建它的用户。然后使用HABTM表来跟踪签名者。

For the User class I would define...

对于User类,我会定义...

  has_many :created_petitions, :class_name => 'Petition', :foreign_key => 'creator_id'
  has_and_belongs_to_many :signed_petitions, :class_name => 'Petition', :association_foreign_key => 'signer_id'    

And for the Petition class I would define...

对于请愿课我会定义......

   belongs_to :creator, :class_name => 'User'
   has_and_belongs_to_many :signers, :class_name => 'User', :foreign_key => 'signer_id'

Then the migration would be...

然后迁移将是......

create_table :petitions_signers, :id => false do |t|
  t.integer :petition_id
  t.integer :signer_id
end

add_column :petitions, :creator_id, :integer

#2


1  

Polymorphism is when you're dealing with relationships between one entity and a number of unrelated entities. If it's between one entity and a number of similar entities, you may want to use STI instead.

多态性是指处理一个实体与许多不相关实体之间的关系。如果它在一个实体和许多类似实体之间,您可能希望使用STI。

Since you're talking about a simple User to Petition relationship, there's no reason to worry about polymorphism. What you need is a User model, a Petition model, and a UserPetition join model that records information about what their relationship to the petition is, such as creator or signer.

既然你在谈论一个简单的用户与请愿关系,那么没有理由担心多态性。您需要的是用户模型,请愿模型和UserPetition连接模型,它记录有关他们与请愿书的关系的信息,例如创建者或签名者。