I h开发者_运维问答ave a Boards model and it has many Attachments. I want to be able to have ONE featured attachment. I have a featured_attachment_id column on the Boards table.
When I'm editing a board, I can select which attachment is the featured one. I'm doing this with a Radio button. Because each attachment is already persisted, I can easily get it's id and make it the featured attachment.
The problem is when I'm creating boards or I want the featured attachment to be a new one. Since it has no id, I have no idea how to know that this new attachment will be the featured one.
Any help on getting this done for new records? I'm using the Board.accepts_nested_attributes_for :attachment, so I can have the board_form.fields_for :attachments ...
Thanks a lot,
Nicolás Hock Isaza
It sounds like you have two different use cases:
- make one of the current attachments featured
- make new attachment and mark it featured
It sounds like you've already implemented a working version of #1 but need to build #2.
I suggest adding a new control on your upload form, perhaps a check box, that lets you flag that new upload as featured. You should be able to correlate that to your new upload in the controller and mark that attachment as featured after you've saved the attachment. You can't do this by attachment ID, but you should be able to do it by ordinal or other method (ie: 3 uploads, mark third as featured).
精彩评论