ruby 将回形针附件从一个模型复制到另一个模型? [英] ruby copy a paperclip attachment from one model to another?

查看:33
本文介绍了ruby 将回形针附件从一个模型复制到另一个模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个这样的模型:-

I have two models like this:-

模型 1 - 卡片 - 包含对首页感兴趣的数据的表示
附件名称 = cardimage
模型 2 - 用户 - 包含用户
附件名称=头像

Model 1 - card - contains a representation of data of interest for front page
attachment name = cardimage
Model 2 - user - contains the user
attachment name = avatar

当我创建时!一张新卡片,我希望将用户模型中的头像作为新卡片图像复制到卡片模型中.

When I create! a new card, I want the avatar from the user model to be copied to the card model as a new cardimage.

是否有一个简单的单衬?

Is there a simple one liner for this?

Ruby/Rails/Paperclip

Ruby/Rails/Paperclip

推荐答案

这应该可以解决问题,如果模型关联,您可以使用 after_create 回调,否则我建议在创建卡片的控制器动作.

This should do the trick, you could use an after_create callback if the models are associated, if not I would recommend doing it in the controller action that creates the card.

instance_of_model_one.cardimage = instance_of_model_two.avatar
instance_of_model_one.save

这篇关于ruby 将回形针附件从一个模型复制到另一个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆