Rails 请教 HABTM 和 Nested Attributes 使用的问题

匿名 · 2013年02月03日 · 1820 次阅读

class Article < ActiveRecord::Base attr_accessible :title, :content has_and_belongs_to_many :photos accepts_nested_attributes_for :photos end

class Photo < ActiveRecord::Base attr_accessible :name, :image has_and_belongs_to_many :articles mount_uploader :image, ImageUploader validates_presence_of :image end

class CreateArticlesPhotos < ActiveRecord::Migration def change create_table :articles_photos, id: false do |t| t.integer :article_id t.integer :photo_id end end end

打算使用 nested_form 在 article form 中进行添加、删除 article 与 photo 的关联(associated)。因为 photo 里面的图片是在别的地方已经上传好了的。article form 里面只是希望增加(删除)photo 与它的关联。反复查看 nested_form 文档和例子,普遍都是 2 个 model 同时创建、修改,没有只是维护他们中间的关系表。请教有什么方法解决这个问题?谢谢。

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号