开发者

Updating several records for same object

Ok I'm really feeling I'm missing the rails way on this one. Following my last question rails parameters in form_for, I can correctly update the message contents but am struggling with updating the recipients

My Draft model

class Draft < ActiveRecord::Base

belongs_to :message
belongs_to :draft_recipient, :class_name => "User"
delegate :created_at, :subject, :user, :body, :draft_recipients,  :to => :message
...

My Message Model

class Message < ActiveRecord::Base

belongs_to :user
has_many :recipients, :through => :message_copies
has_many :draft_recipients, :through => :drafts
has_many :message_copies
has_many :drafts, :class_name => "Draft", :foreign_key => :message_id

attr_accessor :to #array  of people to send to
attr_accessible :subject, :body, :to, :recipients, :author, :user
...

In my controller I want to do something like

new_draft_recipients = params[:draft][:draft_recipients].split(",")
@draft.update_attributes(:draft_recipients => new_draft_recipients)

which obviously doesn't work. When I try update each record comparing old (from the dat开发者_高级运维abase )and new recipients (passed through the form), the algorithm gets ridiculously complicated. I feel what is missing is proper associations, but I don't manage to understand which. I know this is really simple. Thanks for your help

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜