I keep running into errors using this code.
def create
new_post
@post.save!
(@post.users.uniq - [current_user]).each do |user|
Notification.create(recipient: user, actor:current_user, action: "posted", notifiable: @post)
end
redirect_to post_index_path
end
I am not sure why the error occurs but the error message suggests I use "user" instead of "users" but I can't use uniq doing that. I also get more errors doing it.
Edit: Here is my posts model:
class Post < ActiveRecord::Base
has_many :comments
belongs_to :user
belongs_to :subject
validates_presence_of :content
end
I also have a subjects model:
class Subject < ActiveRecord::Base
has_many :posts
has_many :users, through: :posts
validates_presence_of :name
validates_uniqueness_of :name
end
User model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :name, presence: true
has_many :listings, dependent: :destroy
has_many :purchasing, class_name: "Transaction", foreign_key: "buyer_id", dependent: :destroy
has_many :sell, class_name: "Transaction", foreign_key: "seller_id", dependent: :destroy
has_many :purchased, class_name: "Archive", foreign_key: "buyer_id", dependent: :destroy
has_many :sales, class_name: "Archive", foreign_key: "seller_id", dependent: :destroy
has_many :selling_rooms, class_name: "Room", foreign_key: "seller_id", dependent: :destroy
has_many :buying_room, class_name: "Room", foreign_key: "buyer_id", dependent: :destroy
has_many :comments, through: :posts
has_many :posts
has_many :notifications, foreign_key: :recipient_id
def can_buy?(listing_price)
if self.points >= listing_price
true
else
false
end
end
def withdraw(listing_price)
self.points -= listing_price
end
def purchasing_list
purchasing.includes(:seller, :listing)
end
def purchased_list
purchased.includes(:seller, :listing)
end
def sell_list
sell.includes(:seller, :listing)
end
def sales_list
sales.includes(:seller, :listing)
end
end