每次创建新用户时,Rails 都会将数据加载到应用程序中
我试图找出“正确”的方法来做到这一点。
在我的应用程序中,每次创建新用户时,我都希望将特定数据加载到关联的表中。关联表是不同的预设列表。因此,用户有许多列表,这些列表有许多项目。我想在创建用户时加载其中一些项目。
我真的不知道该怎么做,但我猜是这样的: 创建用户 after_create:创建列表(需要列表名称已有的数据) after_create List 然后使用这些列表的现有数据填充 Items 表。
我应该为此使用种子数据吗?这对于生产来说可以吗?如果可以的话,我到底该怎么做呢?
非常感谢任何指导或其他建议。
I am trying to figure out the "right" way to do this.
In my application every time I create a new user I want to have specific data loaded into an associated table. The associated table is a different preset lists. So a User has many lists and those lists have many items. Some of those Items I want to be loaded when the User is created.
I really don't know how to go about doing this but I am guessing something like:
create User
after_create: create Lists (need already existing data for the list names)
after_create List then populate Items table with existing data for these Lists.
Should I be using seed data for this? Is that alright for production and if so how exactly would I go about doing that.
Any guidance or other recommendations are greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
种子实际上用于在应用程序的首次部署时插入数据。
如果您要创建附加到特定事件的记录,那么您的方法可能是最好的方法。如果有大量数据,您可以将其外部化为 YAML 或类似的内容,但 after_create 在多种情况下对我来说效果很好。
有时效果很好的一个方便的技巧是在系统中拥有一个规范用户 - 加载此数据并为当前用户克隆它。
Seed is really for inserting data on the first deployment of the application.
If you are creating records attached to specific events, then your approach is probably the best way to go. If there is a LOT of data, you can externalize it into YAML or something along those lines, but the after_create works fine for me in several situations.
One handy trick that sometimes works well is to have a canonical user in the system - load this data and clone it for you current user.
使用种子实际上是为了插入应用程序所需的所有数据(例如列表)。
对我来说这是个好方法。我没有任何指针。例如,在 redmine 中有一个引导插入。
The using of seed is really made for insert all data needed by your application like your list.
For me it's the good way. I don't have some pointer. By example, In redmine there are a bootstrap insertion.