首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一个数据库记录的Rails多态模型

一个数据库记录的Rails多态模型
EN

Stack Overflow用户
提问于 2015-08-30 21:47:32
回答 1查看 67关注 0票数 0

我正在为一个慈善机构开发一个rails应用程序,该慈善机构为FamiliesFundraisers以及慈善组织本身接受群众资助的捐款。现在,捐款是属于家庭的&使用多态性的募捐者,但我不知道如何为慈善机构筹款。

我应该为慈善机构做一个捐赠的榜样吗?这对我来说是个糟糕的架构,因为它将是一个单一的数据库记录。

接受慈善捐款的最佳方式是什么?

这是我的捐赠模式:

代码语言:javascript
复制
#app/models/donation.rb
class Donation < ActiveRecord::Base
  has_secure_token
  belongs_to :family, via: :recipient
  belongs_to :fundraiser, via: :recipient

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2015-08-30 23:54:40

在您的迁移过程中,您应该做如下的事情

代码语言:javascript
复制
class Donations < ActiveRecord::Migration
  def change
    create_table :donations do |t|
      ...
      t.integer :something_id # this is the record id of something 
      t.string :something_type # this is the model name like family or fundraiser

      t.timestamps
    end
  end
end

现在在你的模型里

代码语言:javascript
复制
class Donation < ActiveRecord::Base
  belongs_to :something, polymorphic: true
end

# repeat this in all the models that can use this polymorphic association 
class Family < ActiveRecord::Base
  has_many :donations, as: :something
end

我希望这会有所帮助。

快乐黑客

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32301469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档