0

Hi I have a few chunks of codes which are badly written. I don't know how I should go about doing this.

Firstly there's a link which works fine:

<td><%= link_to 'Show', bidders_assignments_path(:assignment_id => assignment.id), :method => :post %></td>

That will link to the method bidders in assignment_controller:

def bidders
  @bids = Bid.where(bidders_params).find_each
  #I suspect there's error in the lines below
  @bids.each do |bid|
    @bidders = User.where(user.id => bid.user_id).find_each
  end
end

def bidders_params
  params.permit(:assignment_id)
end

Once the @bidders array is filled with data, it will be listed on the view:

<% @bidders.each do |bidder| %>
  <tr>
    <td><%= bidder.gender %></td>
    <td><%= bidder.experience %></td>
    <td><%= bidder.expected_salary %></td>
    <td><%= bidder.education_id %></td>
<% end %>

I suspect the error is in the filling of @bidders array with data but I can't be sure that's why I'm here. Thanks in advance!

8
  • What is the error? I suspect it's this @bids = Bid.where(bidders_params).find_each because it should be passed a block Commented Jul 19, 2016 at 13:29
  • @j-dexx I changed the def bidders as per Arup Rakshit's suggestion to: def bidders bidders_ids = Bid.where(bidders_params).pluck(:user_id) @bidders = User.where(id: bidders_ids) end Now there's no error but no data get displayed. There's no issue with : @bids = Bid.where(bidders_params).find_each The issue is the line after I believe. Commented Jul 19, 2016 at 13:34
  • How are you determining there is no issue? Could it be that there is simply no data in the database matching the passed arguments? Commented Jul 19, 2016 at 13:37
  • @j-dexx There is data in the database. I've checked that already. Could it be my way of assigning values to the array @bidders? Commented Jul 19, 2016 at 13:40
  • bidders_ids is an array of :user_id and for each of the bidders_ids, there's name of bidders, contact number, and more. So @bidders has got to be a multidimensional array. I just don't know if there's a syntax error in assigning values to the array or displaying the array. Commented Jul 19, 2016 at 13:43

1 Answer 1

1

You need to make an SQL IN query. That will fix your code:

def bidders
  bid_ids = Bid.where(bidders_params).pluck(:user_id)
  @bidders = User.where(id: bid_ids)
end

Use instance variables to expose data from controller to view. If you need a variable to do some temporary calculations use local variables.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but I don't think that's the error. I think the issue is with the <% @bidders.each do |bidder| %> part
I think I didn't declare the array bidders properly. There could be no data inside bidders.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.