I'm a little new to back-end programming...I'm currently running the following code in my rails 4 app to show a basic list of all the admins on a project (if there are any)...
<% if project.projectadmins.any? %>
<div class="row-fluid">
<% project.projectadmins.each do |user| %>
<div class="collaborator">
<%= link_to user do %>
<%= image_tag user.image_url(:thumb).to_s, :class => "profile-pic-thumb" %>
<% end %>
</div>
<% end %>
</div>
<% end %>
However, I also have projectcollaborators for each project, so I'd like to know what the most effective way would be to combine those and provide a list of both projectadmins AND projectcollaborators (with project admins being listed first if there are any...other than that ordering is not important).
I assume the if statement at the beginning would change to...
<% if project.projectadmins.any? || project.projectcollaborators.any? %>
but I'm not 100% sure and am lost on the rest...any help is much appreciated.
You could create a scope, for example project_admins_and_collaborators, which gets all the needed records and then use it in your loop.
You can also do this in following way
project_admins_and_collaborators = project.projectadmins
project_admins_and_collaborators << project.projectcollaborators
project_admins_and_collaborators.flatten.uniq do |user|
#your code
end
Related
I am working on a layout that requires a couple of checks to the database, but I cannot figure out the logic on the IF statement. I have a People table where each record holds a Position field. There are times that specific positions will be held by two people. In which cases they want to be Co-Position. So I current am listing said Position on my page with:
<li>
<% #people.each do |person| %>
<% if person.position == 'Office1' %>
<div class="image">
<%= link_to(person_path) do %>
<%= image_tag("profiles/#{person.uname}S.jpg") %>
<% end %>
</div>
<div class="body2">
<h4>Office</h4>
<%= link_to(person_path) do %>
<h5><%= person.fname %> <%= person.lname %></h5>
<% end %>
</div>
<% end %>
<% end %>
</li>
Now within that I need to run a check at <h4>Office</h4> to see if another record holds Office2, but if I place an IF statement around the H4 tags will it be checking the already fetched record or will it check the entirety of the Person database? Do I need to do this check first and then do an IF/Then or can this be done in a more streamlined way?
Basically I need to check if Office2 is present somewhere in the Position field, and if it is I need the H4 to read Co-Office, else I need it to read Office.
If your condition is if person.position.include?('Office2') then no, it won't touch the database again. The person object is loaded with all its attributes.
Rails each do method is acting strangely and I do not know why.
controller
def index
#fabric_guides = FabricGuide.with_attached_image.all.order(:name)
end
index.html.erb
<div class="guide-items">
<%= #fabric_guides.each do |fabric| %>
<div class="guide-container">
<%= link_to fabric_guide_path(slug: fabric.slug) do %>
<%= image_tag fabric.image if fabric.image.attached? %>
<% end %>
<div class="guide-info">
<p class="g-name">
<%= link_to fabric.name,
fabric_guide_path(slug: fabric.slug) %>
</p>
</div>
</div>
<% end %>
</div>
I have two FabricGuide records so I expect two "guide-container" but I get three. Or more precisely I get two guide containers and a third block of text containing all the content from the last FabricGuide record.
I have almost an identical setup for articles and have never encountered this problem. I'd happily share more information if needed. Thank you!
Please remove = equal sign from your each loop of view code
like below :-
<% #fabric_guides.each do |fabric| %>
...
...
<% end %>
you have used this <%= #fabric_guides.each do |fabric| %> in your view that's why it shows all record in DOM.
The expression for erb tags is <% %>
now if we want to print that tag too then we apply <%= %>
<% consents_checkboxes.each do |checkbox| %>
<%= checkbox.html_safe %>
<% end %>
Hello there,
can i give them a class while looping through them? I can't get it to work and tried several different ways.
This is something I would like to achieve
<% consents_checkboxes.each do |checkbox| %>
<%= checkbox.html_safe, class: 'checkbox' %>
<% end %>
thank you
You can only do it with an element. What you want to do is:
<% consents_checkboxes.each do |checkbox| %>
<p class="checkbox"><%= checkbox.html_safe %></p>
<% end %>
Of course, you can use another element (span, div etc.).
What's on consents_checkboxes? You should provide more context when you ask for something...
It looks like you have strings with the html code, right? you will have to parse the string with something like nokogiri and add a class
<%= Nokogiri.parse(checkbox).add_class('checkbox') -%>
Or you could modify the process that generates that consents_checkboxes to include the class you need. Maybe there's better options, but with only that information it's really hard to tell.
I'm new to rails and I'm trying to build a view that will list the parents and related children
Ex:
Passport has many Visas
I want to list information about the passport and the visas that the passport has.
So I have
<% #passport_list.each do |passport| %>
# passportFields
<% passport.visas.each do |visa| %>
<%= t.text_field :visa_type %>
<% end %>
<% end %>
I'm getting the error
undefined method `visa_type' for #Passport:0x000000091b8b28
It looks like rails is trying to find the property visa_type for passport, instead of in visa. How does the scope work within each? Can I force it to access visa_type from visa?
I think you're looking for the fields_for form helper. This will allow you to create fields for the relevant visa attributes. Replace your code sample with the following, and you should be all set.
<% #passport_list.each do |passport| %>
# passportFields
<% t.fields_for :visas do |visa_fields| %>
<%= visa_fields.text_field :visa_type %>
<% end %>
<% end %>
You can also iterate over the list as follows:
<% #passport_list.each do |passport| %>
# passportFields
<% passport.visas.each do |visa| %>
<% t.fields_for :visas do |visa_fields| %>
<%= visa_fields.text_field :visa_type %>
<% end %>
<% end %>
<% end %>
For more information on fields_for, check out the link I added above, and to customize further for your use case, check out the "One-to-many" section.
IMO you should always handle the null case of an object.
Something like this if you use rails (present? is a Rails function)...
<% if #passport_list.present? %>
<% #passport_list.each do |passport| %>
passportFields
<% passport.visas.each do |visa| %>
<%= t.text_field :visa_type %>
<%end%>
<%end%>
<% else %>
<p>Nothing to see here</p>
<% end %>
However if your #passport_list is backed by an ActiveRecord Query, you can handle this in the model/helper/controller by returning the .none query on the model. Note that this differs from an empty array because it is an ActiveRecord Scope, so you can chain AR queries onto it
# scope on AR model
def self.awesomeville
where(country_of_origin: "awesomeville")
end
# method queried in controller
#passport_list = Passport.all
if #passport_list.present?
#passport_list
else
Passport.none
end
# additional filtering in view is now possible without fear of NoMethodError
#passport_list.awesomeville
Whereas a ruby Array would raise an error as it would respond to the Array methods.
Looking for ways to improve the particularly bad performance I'm getting from my rails application. Here's the code from the page in question:
notifications_controller.rb
class NotificationsController < ApplicationController
def index
#questions = Question.all.order(:updated_at => :desc)
#users = User.all
#answers = Answer.all.order(:updated_at => :desc)
end
end
and here's the corresponding view. I know it's ugly but it's working.
<div>
<% if current_user %>
<div class="notifications-added col-md-8">
<h4 class="col-md-offset-2">Approvals & Answers</h4>
<span class="text-center">
<% current_user.questions.order(id: :desc).each do |question| %>
<% if question.approved == true %>
Your question, <%= link_to "#{question.title}", question_path(question) %>, has been <span class="notifications">approved.</span><br>
<% end %>
<% question.answers.each do |answer| %>
<%= answer.user.name %> <span class="notifications">added an answer</span> to your question, <%= link_to "#{question.title}", question_path(question) %>.<br>
<% end %>
<% end %>
</span>
</div>
<div class="notifications-voted col-md-4">
<h4 class="text-center">Votes</h4><span></span>
<% current_user.answers.order(updated_at: :desc).each do |answer| %>
<% #users.each do |user| %>
<% if user.voted_up_on? answer %>
<%= user.name %> <span class="notifications">upvoted</span> your answer to <%= link_to "#{Question.find(answer.question_id).title}", question_path(answer.question_id) %>.<br>
<% elsif user.voted_down_on? answer %>
<%= user.name %> <span class="notifications">downvoted</span> your answer to <%= link_to "#{Question.find(answer.question_id).title}", question_path(answer.question_id) %>. <br>
<% end %>
<% end %>
<% end %>
</div>
<% end %>
</div>
I think I'm just sorting too much. The page is taking a long time to load. What's the low hanging fruit for improving my performance? Any thoughts? Thanks in advance.
The first , you should move logic code in your view into model.
The second , use pluck method instead array active record objects. Array string are lightweight than array of active record. Right ?
The third , use slim template engine instead erb template engine.
The fourth , cache db.
The fiveth, use google PageSpeed plugin for google chrome to analytic what's slow.
Peter answer is correct, and I will add two things :
Eager loading
Doing this
current_user.questions.each do |question|
question.answers.each do |answer|
...
end
end
will generate a query for each question. Rails will load all the questions, then for each question load its associated answers (1 query + 1 query for any question).
If you replace the first line by
current_user.questions.include(:answer).each do |question|
Rails will load all the questions, then all the associated answers (2 queries).
Look at the log
Every information for any bad performance should be visible on logs in development mode. For example, if voted_up need to load any other models than answer, your query number will be too big.