I have a model called Notifications, which basically acts as a join table between Requests and 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. What am I doing wrong here?
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
class Notification < ActiveRecord::Base
attr_accessible :accommodation_id, :request_id
has_one :request
end
models/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
You don’t define @request or :location in your controller. Assuming it’s a Request object:
Just like @AnomalousThought said: