Returning issue in Comment::ActiveRecord_Associations_Collection

喜欢而已 提交于 2019-12-12 02:49:40

问题


I'm trying to display all the comments made from a user in a User profile page (no matter if it's a public or a page behind the login), but I'm experiencing an issue that prints in my app Comment::ActiveRecord_Associations_CollectionProxy:0x00000103c89750 (it's not an error that kills my app, just prints that message).

the only element commentable within my app are 'hacks', and users can create comments on each hack.

user.rb

      class User < ActiveRecord::Base
      TEMP_EMAIL_PREFIX = 'change@me'
      TEMP_EMAIL_REGEX = /\Achange@me/
      include Gravtastic
      gravtastic

      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :confirmable

        validates_format_of :email, :without => TEMP_EMAIL_REGEX, on: :update

      def self.find_for_oauth(auth, signed_in_resource = nil)

        # Get the identity and user if they exist
        identity = Identity.find_for_oauth(auth)

        # If a signed_in_resource is provided it always overrides the existing user
        # to prevent the identity being locked with accidentally created accounts.
        # Note that this may leave zombie accounts (with no associated identity) which
        # can be cleaned up at a later date.
        user = signed_in_resource ? signed_in_resource : identity.user

        # Create the user if needed
        if user.nil?

          # Get the existing user by email if the provider gives us a verified email.
          # If no verified email was provided we assign a temporary email and ask the
          # user to verify it on the next step via UsersController.finish_signup
          email_is_verified = auth.info.email && (auth.info.verified || auth.info.verified_email)
          email = auth.info.email if email_is_verified
          user = User.where(:email => email).first if email

          # Create the user if it's a new registration
          if user.nil?
            user = User.new(
              name: auth.extra.raw_info.name,
              #username: auth.info.nickname || auth.uid,
              email: email ? email : "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com",
              password: Devise.friendly_token[0,20]
            )
            user.skip_confirmation!
            user.save!
          end
        end

        # Associate the identity with the user if needed
        if identity.user != user
          identity.user = user
          identity.save!
        end
        user
      end

      def email_verified?
        self.email && self.email !~ TEMP_EMAIL_REGEX
      end

      include TheComments::User

      has_many :hacks
      has_many :comments

      def admin?
        self == User.first
      end

      def comments_admin?
        admin?
      end

      def comments_moderator? comment
        id == comment.holder_id
      end
    end

comment.rb

    class Comment < ActiveRecord::Base

      belongs_to :user

      include TheComments::Comment
      # ---------------------------------------------------
      # Define comment's avatar url
      # Usually we use Comment#user (owner of comment) to define avatar
      # @blog.comments.includes(:user) <= use includes(:user) to decrease queries count
      # comment#user.avatar_url
      # ---------------------------------------------------

      # public
      # ---------------------------------------------------
      # Simple way to define avatar url
      #
      # def avatar_url
      #   src = id.to_s
      #   src = title unless title.blank?
      #   src = contacts if !contacts.blank? && /@/ =~ contacts
      #   hash = Digest::MD5.hexdigest(src)
      #   "https://2.gravatar.com/avatar/#{hash}?s=42&d=https://identicons.github.com/#{hash}.png"
      # end
      # ---------------------------------------------------

      # private
      # ---------------------------------------------------
      # Define your content filters
      # gem 'RedCloth'
      # gem 'sanitize'
      # gem 'MySmilesProcessor'
      #
      # def prepare_content
      #   text = self.raw_content
      #   text = RedCloth.new(text).to_html
      #   text = MySmilesProcessor.new(text)
      #   text = Sanitize.clean(text, Sanitize::Config::RELAXED)
      #   self.content = text
      # end
      # ---------------------------------------------------
    end

hack.rb

    class Hack < ActiveRecord::Base

      belongs_to :user

        acts_as_taggable # Alias for acts_as_taggable_on :tags
        acts_as_taggable_on :tags
        has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
        validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

      #For commenting
      include TheComments::Commentable

      # Denormalization methods
      # Please, read about advanced using
      def commentable_title
        "Undefined Post Title"
      end

      def commentable_url
        "#"
      end

      def commentable_state
        "published"
      end

    end

user view

            <p>
              <strong>User email?:</strong>
              <%= @user.email %>
              <%= @user.comcoms %>
            </p>

The gem I'm using for comments is The_Comments and there are some docs here that I have read widely, and I think @user.comcoms should return what I'm looking for but not :/


回答1:


@user.comcoms will give all comments related to the particular users(@user in your case) commentable models. So, its going to return a collection of comments and Comment::ActiveRecord_Associations_CollectionProxy:0x00000103c89750 is showing the reference of that collection.

You need to iterate over the collection and display the required fields from an instance of Comment class.

Replace

<%= @user.comcoms %>

With

 <% @user.comcoms.each do |comment| %>
    <%= comment.raw_content %>
    <%= comment.commentable_title %>
    <%# Add other attributes %>
 <% end %>

Refer to the link that you added in the question, for the attributes that can be accessed on Comment Comment API



来源:https://stackoverflow.com/questions/24458375/returning-issue-in-commentactiverecord-associations-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!