need help with this please:
@follows = Follow.where("admin_user_id = ?", session[:id])
This returns records from the Follow table, that has the following coloumns, :admin_user_id, :assignment_id. I would now like to do this:
@assignments = Assignment.where("id = ?", @follows.assignment_id)
The Asssignment table has the following columns, :id, :name.I have tried the “@follows.assignment_id” to substitute each id which i can then use in the view like,
<% @assignment.each do |assignment| %>
<%= assignment.name %>
<% end %>
Thanks in advance for your help
Part of the problem here is your first query:
Follow.where(...)returns a scope, it will not perform the query until you try to access the@followsobject. This is probably not a big deal, but you should probably make it this (unless you are going to dynamically add more conditions):This ensures that @follows is an array of Follow objects. Because it’s an array (and not a single
Follow) there is no assignment_id on it. Change your second query to this:Basically,
.mapreturns an array of the return values of the block instead of the original object it was called on, meaning instead of an array of follows, it returns an array of the assignment ids of each object. When you pass this array as the conditions, it generates a SQL query something like:if your follows have assignment ids 1, 2, and 3.