简体   繁体   English

在联接表中创建新记录

[英]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返回值的值,该值只会为truefalse 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.

相关问题 rails-在连接表中创建记录 - rails- creating record in a join table 通过Rails中的两个不同的控制器创建联接表记录 - Creating a join table record through two different controllers in rails Rails:创建新记录时更新连接表自定义属性 - Rails: Update join table custom attribute when a new record is created 从表单为联接表创建新记录。选择输入 - Create new record for Join Table from a form.select input 新连接表记录上的 rails simple_form 关联 - rails simple_form association on new join table record 创建一种在Rails 3.1中向表添加新记录的方法 - Creating a method to add a new record to a table in Rails 3.1 如何创建新的联接表记录而不创建任何一条记录的新实例。 轨道 - How to create a new joined table record without creating a new instance of either record. Rails 在 Rails 上创建新记录时在连接表上添加额外信息 - Adding extra info on the join table when creating new records on rails Rails 3 - 在创建新的HABTM关联时未更新连接表 - Rails 3 - join table not updated when creating new HABTM association 如何创建多个新记录,检查现有重复项以及将新记录或现有记录添加到联接表中 - How to create multiple new records, checking for existing duplicates, and adding new or existing record to join table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM