I have been struggling with this and after some searching, I haven't been able to find anything online. Users choose from several study session dates (their first choice is the one that is the most convenient for them) and I store their choices as Id's that correspond to the chosen dates. The Id's are stored as an array for each user (ex: [1,3,2,4]). Each study session has a capacity, so what I need to do is sort it by their first choice but if their first choice is over the capacity, I need to place them into their second choice. So far, I have the grouping by first choice done but I don't know how to place all users who are over the capacity into their second choice. Here is my code so far:
<% #choices.all.group_by { |choice| choice.choice.first}.each do |date, choices| %>
<%= #options.where(:id => "#{date}").first.date.strftime('%B %d, %Y') %>
at
<%= #options.where(:id => "#{date}").first.time.strftime('%l:%M %P') %>
<ul>
<% choices.each do |sort| %>
<li><%= sort.name %></li>
<% end %>
</ul>
<% end %>
Thanks for your help!
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.
I have the following situation in my rails project
application.html.erb:
...
<%= render :partial => 'layouts/sidebar' %>
<%= yield %>
...
_sidebar.html.erb:
<% #groups.each do |group| %>
<li><%= link_to group.name, "#" %></li>
<% end %>
Sidebar consists of a list of groups which doesn't change often. That's why i don't want to query the list in DB every time I go to another page in content part (yield). Is there a way how can i preserve the list throughout several pages? I KNOW about the sessions and caches, but maybe there is a better solution.
Thanks
<% #user.chores.each do |chore| %>
<li><%= chore.name %></li>
<% end %>
<% #user.chorelists.each do |chorelist| %>
<li><%= chorelist.day %></li>
<% end %>
Hola, friends.
My app has three models: User, Chore, and Chorelist (which is a 'joined' resource for User and Chore).
Both of these blocks work, but how can I write, say, a conditional statement that will give me all of that user's chores for a specified day? (The days are saved in the Chorelist model as strings.)
If you need anymore more code from the app, just let me know. And thank you!
It'd be great to know what fields do your models have. Meanwhile, I think that you want something like this:
date = Date.today
#user.chores.joins(:chorelist)
.references(:chorelist)
.where(chorelists: {day: date})
And this should be in your controller, yeah, per commenter's suggestion.
Sorry for the amateur question but I am still quite new to rails. I currently have an app that creates jobs and I would like to display the jobs beneath the date they were created in the same way they do on Dribble
At the moment to display the jobs I have te following:
<% #jobs.each do |job| %>
<div class="job-wrapper"><%= link_to user_job_path(job.user_id ,job) do %>
<div class="job">
<div class="job-desc"><strong><%= job.company %></strong> are looking for a <strong><%= job.job_title %></strong>
<div class="job-sal"><%= job.job_salary %></div>
</div>
</div>
</div>
<% end %>
<% end %>
I am sure I need to create a loop of some kind to make this work but am unsure how to incorporate it so that the date only displays once at the top of the jobs and then all jobs created during that date are shown?
Any help would be much appreciated! Thanks
Try the pattern below --
<% #job.group_by{|x| x.created_at.to_date }.each do |date,jobs_on_that_date| %>
<%= date %>
<% jobs_on_that_date.each do |job| %>
# render the job
<% end %>
<% end %>
Basically you need to group your jobs by the date (or whatever you want to group on) then get a hash keyed on the stuff you grouped on. Then render the key (date) followed by the list of objects relating to that key.
Sorry if the title is not enough to understand what i am asking about.
I am rails developer and i used multiple lines of <% %> in my views but now i realized that it's not best practice so i came here and like to you all excellent guys what is the correct way in ROR?
For example if i required to something like following
<% user =User.all %>
<% name= [] %>
<% count = 0 %>
<% for user in users %>
<% name << user.name %>
<% count+=1%>
<% end %>
Can i do it as follows ?
<% user =User.all
name= []
count = 0
for user in users
name << user.name
count+=1
end
%>
I know better way of collecting element from array But above is just example.
But my question is, is it possible and if yes which is the correct way?
I think the correct way is something totally different: move logic out of views.
This blog post explains what I mean.
in start and end must has '<%' or '%>'
Like:
<% users = User.all
name= []
count = 0
for user in users
count+=1
end %>
Using just a single pair of tags per code block is certainly preferable if only because it makes the output smaller.
The code should really rather look like
<% names = User.all.map(&:name) %>
Note that "count" can be obtained via names.size.
If you need to mix <% and <%= you need to switch:
<% for user in User.all %>
<%= user.name %></br>
<% end %>