Displaying 2 results from an estimated 2 matches for "user_votes".
2009 Nov 09
3
How can I improve a Ruby on Rails code that hast a lot of SQL as strings?
...m.find(:all,
# items.* for all the Item attributes, score being the sum of votes,
user_vote is the vote of user_id (0 if no vote) and voter_id is just user_id
for latter reference.
:select => "items.*,
IFNULL(sum(all_votes.value), 0) as score,
user_votes.value as user_vote,
\"#{user_id}\" as voter_id",
# The first join gets all the votes for a single item (to be summed
latter).
# The second join gets the vote for a single user for a single item.
:joins => ["LEFT JOIN votes as all_votes ON...
2006 May 13
4
undesireable lazy loading
...k < ActiveRecord::Base
has_one :user_vote
end
class UserVote < ActiveRecord::Base
belongs_to :user
belongs_to :link
end
I have a SQL query that is responsible for pulling out all links along
with votes from a database:
links = Link.find_by_sql(
["SELECT * FROM links LEFT JOIN user_votes ON links.id =
user_votes.link_id" +
" AND user_votes.user_id = ?", session[:user_id]]
)
If a user hadn''t voted for a given link yet, an empty record is returned
(NULL). The problem is that when trying to call that unexisting
associated object (link.user_vote) Rails a...