You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given models User and Publications where User has_many: publications, I need to iterate over a user's publications by calling {% for publication in user.publications %} and have access to methods defined in PublicationDrop.
user.publications is defined in the UserDrop class as just @user.publications.to_a.
For clarity, here is pseudocode of what I want to do:
Publications Authored by {{ user.full_name}}:
{% for publication in user.publications %}
# These methods should come from PublicationDrop
{{ publication.title }}
# Displays 'No Comments' if 0, otherwise displays number of comments
{{ publication.comment_count }}
{% endfor %}
I saw #64, and I'm sure I could adapt that to my needs. However, it is said in that thread that this method 'can have a huge security and performance impact'.
What I'm wondering is if the method described in #64 is still the best method, and if not, what is?
The text was updated successfully, but these errors were encountered:
If you define Publication#to_liquid to return a PublicationDrop then the iteration should work automatically. When you access {{ publication.title }}, you are effectively accessing publication.to_liquid.title.to_liquid.
I'm not sure if I missed something unique about this question, feel free to follow up.
Performance note: as hinted above, to_liquid is called every time a variable is accessed, so if your to_liquid is expensive to compute then it's a good idea to memoize it.
Given models
User
andPublications
where Userhas_many: publications
, I need to iterate over a user's publications by calling{% for publication in user.publications %}
and have access to methods defined inPublicationDrop
.user.publications
is defined in theUserDrop
class as just@user.publications.to_a
.For clarity, here is pseudocode of what I want to do:
I saw #64, and I'm sure I could adapt that to my needs. However, it is said in that thread that this method 'can have a huge security and performance impact'.
What I'm wondering is if the method described in #64 is still the best method, and if not, what is?
The text was updated successfully, but these errors were encountered: