<% #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.
Related
I have two models, User and Profile, with user_id used as a foreign key to link them. I'd like to put a conditional statement in my footer that looks to see if the current user has a profile. If they do they will see a link to the edit page and, if they don't, to the create/new page.
I tried finding a solution online and I think using the presence_in?(object) method might work but, as a newbie, I don't quite get the syntax.
This is what I have so far if someone can help me get to the finish line :)
<% if current_user.id (something something) %>
<li><%= link_to "Edit Profile", edit_profile_path(:id => current_user) %></li>
<% else %>
<li><%= link_to "New Profile", new_profile_path %></li>
<% end %>
If my question is unclear please let me know and I'll provide a link to my Github page
You can simply do <% if current_user.profile.present? %> to check whether user's profile exists or not. You need have has_one association in User model to get this working e.g has_one :profile
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!
This sounds like an easy question but for some reason I am lost.
In my user's profile page, I'm showing all of their posts. If they have 3 posts, I want to show something like this.
1. Post one title
2. Post two title
3. Post three title
So it shows the number to the left of the post. This cannot be the post's ID though. Do anyone know how to solve this?
Yes, check out Enumerable#each_with_index. So you can do something like:
#posts.each_with_index do |post, index|
puts "#{index} #{post}"
end
In this case you can rely on the HTML for the enumeration. Just use an ordered list.
<ol>
<% #posts.each do |post| %>
<li><%= post.title %></li>
<% end %>
</ol>
For anything more elaborate than this, Enumerable#each_with_index will be your best choice.
In a view I would like to render the amount of people living in a person's house.
The follow code works, but I believe there is a shorter and better way to write the code.
<% if current_user.family_size == 1 %>
<li><%= current_user.family_size %> person</li>
<% else %>
<li><%= current_user.family_size %> people</li>
<% end %>
Thanks in advance.
Use pluralize method:
<li><%= pluralize(current_user.family_size, 'person') %></li>
UPDATE:
If there is a need for pluralized noun without a count, use:
'person'.pluralize(2) #=> 'people'
I think I am deeply misunderstanding how to write instances.
Miniatures have_many Manufacturers through the Productions table.
On the miniatures show page I am trying to list all the manufacturers for the current miniature and have them link the Manufacturer show page. Like so:
<% #miniature.manufacturers.each do |manufacturer| %>
<%= link_to #miniature.manufacturer.name, manufacturer_path %>
<% end %>
Needless to say it does not work. It gives "undefined method `manufacturer'".
I have tried A LOT of different combinations to no avail. The following version puts all the manufacturers, rolled into one link, once for each manufacturer a miniature has, and links to /manufacturers. A big mess.
<% #miniature.manufacturers.each do |manufacturer| %>
<%= link_to #miniature.manufacturers.map(&:name).join(', '), manufacturer_path %>
<% end %>
I have been working on other things and hoping I would get the hang of this but I'm pretty sure it's something pretty fundamental about how I set up the instance.
If it's more likely something I need to add to the controller then I can add my controller code here. Any help much appreciated.
Does this work:
<% #miniature.manufacturers.each do |manufacturer| %>
<%= link_to manufacturer.name, manufacturer_path(manufacturer) %>
<% end %>