[英]Creating a New Record in Join Table
I have a model called Notifications, which basically acts as a join table between Requests and Accommodations. 我有一个称为Notifications的模型,该模型基本上充当Requests和Accommodations之间的联接表。
In my "create" method in my Requests controller, I have: 在我的请求控制器的“创建”方法中,我有:
# find associated accommodations, currently matching: location
@accommodations = Accommodation.where('location' => :location)
@accommodations.each do |accommodation|
@notification = @request.notification.build('accommodation_id' => accommodation.id ).save
end
Which doesn't seem to be creating a new Notification record. 似乎没有在创建新的Notification记录。 What am I doing wrong here?
我在这里做错了什么?
models/accommodation.rb models / accommodation.rb
class Accommodation < ActiveRecord::Base
validates_presence_of :title, :description, :thing, :location, :spaces, :price, :photo
attr_accessible :photo_attributes, :title, :description, :thing, :location, :spaces, :price
has_one :photo
has_many :notifications
belongs_to :user
accepts_nested_attributes_for :photo, :allow_destroy => true
end
models/notification.rb 模型/notification.rb
class Notification < ActiveRecord::Base
attr_accessible :accommodation_id, :request_id
has_one :request
end
models/request.rb 型号/request.rb
class Request < ActiveRecord::Base
attr_accessible :firstname, :lastname, :email, :phone, :datestart, :dateend, :adults, :children, :location, :status
validates_presence_of :firstname, :lastname, :email, :phone, :datestart, :dateend, :children, :adults, :location
has_many :notifications
end
@request.notification.build
should be @request.notification
s .build
since a request has_many notifications. @request.notification.build
应该是@request.notification
的 .build
因为请求具有has_many通知。
Also, you're setting @notification
to become the value of what the .save returns which will only be true
or false
. 另外,您将
@notification
设置为.save返回值的值,该值只会为true
或false
。 You might want to use create
if you want @notification
to point to the actual object. 如果希望
@notification
指向实际对象,则可能需要使用create
。 Additionally, you probably already know that it's going to keep overwriting the @notification
variable on every iteration, which you may or may not want. 此外,您可能已经知道,每次迭代都可能会覆盖
@notification
变量,而您可能想要也可能不想这么做。
There's a lot to be said about the example provided, I think the implementation is entirely poor. 关于所提供的示例,有很多话要说,我认为实施完全不完善。 But that's not relative to the question of why the notification record isn't saving.
但这与为什么通知记录不保存的问题无关。
You don't define @request or :location in your controller. 您没有在控制器中定义@request或:location。 Assuming it's a Request object:
假设它是一个Request对象:
@notifications = Array.new
@accommodations = Accommodation.where('location' => :location)
Accommodation.find_each(:conditions => { 'location' => :location }) do |a|
notification = @request.notifications.create('accommodation_id' => a.id)
@notifications << notifications
end
Just like @AnomalousThought said: 就像@AnomalousThought所说的那样:
There's a lot to be said about the example provided, I think the implementation is entirely poor.
关于所提供的示例,有很多话要说,我认为实施完全不完善。 But that's not relative to the question of why the notification record isn't saving.
但这与为什么通知记录不保存的问题无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.