[英]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才能創建/發布事件。 普通用戶將可以訪問注冊表單來參加這些活動。 請幫我解決這個障礙。
幾件事:
您可以像這樣建模:
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.