2

I have post model

class Post < ActiveRecord::Base
     acts_as_voteable
 end 

and Vote model

class Vote < ActiveRecord::Base

 scope :for_voter, lambda { |*args| where(["voter_id = ? AND voter_type = ?", args.first.id, args.first.class.name]) }
  scope :for_voteable, lambda { |*args| where(["voteable_id = ? AND voteable_type = ?", args.first.id, args.first.class.name]) }
 scope :recent, lambda { |*args| where(["created_at > ?", (args.first || 2.weeks.ago)]) }
 scope :descending, order("created_at DESC")

  belongs_to :voteable, :counter_cache=>true,:polymorphic => true,:touch=>true
   belongs_to :voter, :polymorphic => true

  attr_accessible :vote, :voter, :voteable


 # Comment out the line below to allow multiple votes per user.
 validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id]

end

when I get the post voters with these method

  <% @post.voters_who_voted.each do |voter|%>
  <%= voter.name %>
   <% end %>

I load my database how can I select only the user name and user id from these array?

update I changed my code I am using thumbs_up gem I pasted less code first to simplify the question

3
  • A voter can be any user (not just the user to whom the post belongs?) Commented Aug 20, 2013 at 17:04
  • the user is the voter who voted for the post Commented Aug 20, 2013 at 17:05
  • 1
    updated answer. Take a look at source code of this gem. It can be helpful Commented Aug 20, 2013 at 17:50

2 Answers 2

1

What do you mean by "load database"? If you want to select only id and name columns, then use @post.users.select([:id, :name]).each ...

Or is it about this problem (according to code that you provided)?

UPD.

voters_who_voted loads all voters and returns array https://github.com/bouchard/thumbs_up/blob/master/lib/acts_as_voteable.rb#L113. You have to add own association to Post model:

has_many :voters, :through => :votes, :source => :voter, :source_type => 'User'

It's just example, perhaps voters will clash with already existing method, if any.

Then use it here instead of voters_who_voted

Sign up to request clarification or add additional context in comments.

3 Comments

sorry it was a typo also select method with your code gives me ArgumentError: wrong number of arguments(1 for 0)
Seems like @post.users is returning array instead of ActiveRecord::Relation object. Maybe it has already been loaded before? You should check your controller code and view code above this part or post it here
your method gives me ActiveRecord::HasManyThroughAssociationPolymorphicSourceError: Cannot have a has_many :through association 'Post#voters' on the po lymorphic object 'Voter#voter'.
1

did you try collect method ??

names = @post.users.collect(&:name)

ids = @post.user.collect(&:id)

If you want it to be related you can make a HASH with it. Id's mapped to the names.

2 Comments

Use @post.users.pluck(:name) in this situation
i guess the difference is pluck directly queries only that particular field and collect will collect from the whole row of data. having @post.users in a variable and collecting will make only one query to the db instead of two , since we need name and id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.