Ruby on Rails Monday, June 30, 2014

On 30 June 2014 09:57, Fresh Mix <lists@ruby-forum.com> wrote:
> Colin Law wrote in post #1151111:
>
>> If you iterate through the blogs then for each blog
>> visits_count = blog.visits.count
>
> Thanks, its works!
>
> But now I have added "liked" column to a visits-table:
>
> Visits (id, user_id, blog_id, page_id, liked)
>
> Default value is false but if user click "like"-link on a page its come
> to true.
>
> But how can I count all likes?
>
> page_likes_count = page.visits.liked.count Does not seem to work...

Think about it. page.visits is an activerecord association
(effectively an array) of visits. You can call count on that which
tells you how many in the array, but you can't call liked as that is a
member of a single Visit object. You can call where() on an
association however and then call count on that, so that is one way to
do it. How are you getting on with the tutorial? It would answer all
these basic questions for you so we do not need to use our time
helping you when you could help yourself.

Colin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsZMmhuer%2B%2BjQG9KCM6e6eoMdsqKFu-SUCMskfiHJaj%3DQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment