从控制台我可以这样做:
bin = Bin.find(9)
bin.messages.create :to => "joe@example.com", :from => "tom@example.com", :subject => "hi", :body => "just hi"
但是,我正在尝试从我的Message模型中的函数内执行完全相同的操作,并且它保存了一条记录,但是bin_id是nil。这很重要,因为Message属于Bin,Bin有很多消息。
message.rb的内容:
class Message < ActiveRecord::Base
attr_accessible :body, :from, :subject, :to
belongs_to :bin
def self.receive_mail(message)
bin = Bin.find(9)
bin.messages.create :to => "joe@example.com", :from => "tom@example.com", :subject => "hi", :body => "just hi"
end
end
我需要做些什么特别的事情才能从我的Message模型中获取Bin?