Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8361649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:44:38+00:00 2026-06-09T11:44:38+00:00

I’m working in Rails and Activerecord and trying to merge some data from related

  • 0

I’m working in Rails and Activerecord and trying to merge some data from related tables together in my view, here are my models:

class Report < ActiveRecord::Base
  has_many :votes
end

class Vote < ActiveRecord::Base
  belongs_to :reports
end

class User < ActiveRecord::Base
  has_many :votes
end

Each vote has a user and a report.

In my view I need the following, hopefully as easily as possible:

  1. a total number of votes for each report from all users
  2. a true/false if the user has voted on the particular report

Right now, my basic understanding of ActiveRecord queries only takes me as far as creating a helper with the report and the current user and than querying for the existence of report

Same goes for counting the total number of votes for all users for a report as follows:

Controller

def index
  #this is where I need some help to get the related information into a single
  #object
  @reports = Report.where('...')
end

View

<% @reports.each do |report| %>
    <% if(hasVoted(@current_user.id, report.id)) %>
        <!-- display the 'has voted html' -->
    <% end %>
<% end %>

Helper

def hasVoted(current_user_id, report_id)
    if(Vote.exists?(:user_id => current_user_id, :report_id => report_id))
      true
    else
      false
    end      
end

Hope that gives you some insight into helping…thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:44:39+00:00Added an answer on June 9, 2026 at 11:44 am

    Sure.

    Firstly, please consider naming your method has_voted? instead of hasVoted. Secondly, consider moving that method in the user model.

    #user.rb
    def voted_on?(report_id)
      votes.where(:report_id => report_id).exists?
    end
    

    Your view will then read

    <% if current_user.voted_on?(report) %>
      ...
    <% end %>
    

    The other question you had was to find the number of votes a report has received. This is simple too. You could do this in your view inside the loop where you iterate over @reports

    <% vote_count = report.votes.size %>
    

    Please keep in mind that his would result in N queries (where N = number of reports). Since you are new to Rails i’m not going to complicate your Reports query in the controller where you fetch you reports to include the vote count (unless you ask me to). But once you are comfortable with what happening in here, thats where you would optimize.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a view passing on information from a database: def serve_article(request, id): served_article
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.