0

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
CJK
  • 39
  • 1
  • 7
  • what are you trying to accomplish? A post belongs to a `user` logically unless you have something different in your application. So, you can't call `users` method on a post object – Arun Kumar Mohan Sep 21 '16 at 21:55
  • Hi and welcome to Stack Overflow. Can you give us some of the code related to this? eg how does a post relate to a user? Does a post have more than one user or only one? can you show us the full error message? Can you look in your server logs (usually `log/development.log` or on the console screen) and show us the three or four lines of filenames that come directly after the error message? (these help us pinpoint the exact line of code throwing the error). – Taryn East Sep 21 '16 at 21:55
  • Please include Post and User models (at least relevant parts) – Mike Szyndel Sep 21 '16 at 22:04
  • @ArunKumar I am not sure i understand. why can't you call users on a post object. – CJK Sep 22 '16 at 00:44
  • @CJK Because a `post` belongs to a single `user` and not multiple users. You are able to call `@post.user` because of the relationship `belongs_to :user` in `post.rb` – Arun Kumar Mohan Sep 22 '16 at 00:46
  • @ArunKumar well a user has many posts, so I was looking to call each user and their respective post – CJK Sep 22 '16 at 01:13

1 Answers1

1

I am guessing you did not declare the relationship the right way; Do you have in your Post model ?:

has_one :user 

Or did you declare

has_many :users

The current behavior is expected if it is the first one. For your desired situation, it logically makes sense to have in your user model (user.rb)

has_many :posts

and in your Post model

belongs_to :user
codigomonstruo
  • 1,081
  • 1
  • 11
  • 45
  • I have a has_many :users in a subjects model they connected as shown in the edit – CJK Sep 21 '16 at 23:55