簡體   English   中英

如何構建獨特的數據庫模型Ruby on Rails 4

[英]How to build a unique database model Ruby on Rails 4

我目前有三種模型:支持Devise auth gem的兩種不同的用戶模型:社區組織者和常規用戶,以及一種社區事件模型。

我正在嘗試允許社區組織者創建事件:

  • 用戶可以向其捐贈資金;
  • 哪些用戶可以參加。

我使用stripe作為支付網關。 到目前為止,我已經為Event模型設置了控制器/模型支架:

class Event < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, use: :slugged
  belongs_to :org_user #organization_user
  has_one :category
end

這是我的組織用戶模型:

class OrgUser < ActiveRecord::Base
  extend FriendlyId
  friendly_id :username, use: :slugged
  has_many :posts
  has_many :comments
  has_many :events
 end

有人告訴我在我的事件和org_user模型中可能需要這些特定的模式

   class Event < ActiveRecord::Base
    event has_many :attendances
    event has_many :users, through: :attendances
   end

  class OrgUser < ActiveRecord::Base
    org_user has_many :attendances
    org_user has_many :events, through: :attendances
    end

class User < ActiveRecord::Base
end

您對建立關系有何建議? 我也不想忘記User.rb,它將在表單上簽名,成為:org_user發布的事件的一部分。
只有org_user才能創建/發布事件。 普通用戶將可以訪問注冊表單來參加這些活動。 請幫我解決這個障礙。

幾件事:

  • 正確的關系關系是錯誤的(請參閱doc
  • 您沒有建立從用戶到事件的捐贈關系模型

您可以像這樣建模:

  class Event < ActiveRecord::Base
    has_many :attendances
    has_many :users, through: :attendances
  end

  class OrgUser < ActiveRecord::Base
    has_many :events # as coordinations
  end

  class User < ActiveRecord::Base
    has_many :donations
    has_many :attendances
  end

  class Attendance < ActiveRecord::Base
    belongs_to :user
    has_one :event
  end

  class Donation < ActiveRecord::Base
    belongs_to :user
    has_one :event
  end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM