views:

84

answers:

1

I have a person model and a quotes model. Each person can have many quotes associated with them, which in turn has things like Author, Text, Url associated with them. In practice really only one person has a quotes object but all people could potentially have one.

I'm trying to have a dirt simple bulk moderate tool on quotes. As in, users can type in quotes and an admin can go and have all of the quotes of a particular person (but like in practice, only one will have them) and check a box which sets a boolean approved to 0 or 1. When the quotes are actually displayed, they're filtered by this field.

I'm getting really mixed up trying to create a form that updates these all in bulk. I got the form to display all of them as so:

<% form_for :quotes, :html => { :method => :put }, :url => {:controller => "quote", :action => "bulk_update", }  do |quote_form| %>
   <% @person.quotes.each do |quote| %>
<p>
   <% fields_for "quotes[id][]", quote  do |fields| %>
           Text....
           <%= fields.check_box :approved %><br><br>

  <% end %>
</p>

<% end %>
<%= submit_tag 'Update' %>
<% end %>

I just have an incredibly generic thing in my bulk update controller which is basically like a normal update controller. I'm not sure what to put in there. I could try to put all the quotes which I probably should but I don't know how to get to that variable anyway because I'm doing all of this from whithin the Person view etc. The controller is osmething like this:

def bulk_update
@quote = Quote.find(params[:id])
end

I guess I'm getting confused about the ID that should be in there and what should be getting passed in. Should the person ID be part of this at all? Like I said, I don't really care if technically all quotes are displayed on that page, they don't need to be sorted by user.

I'm getting parameters that look like this:

"quotes"=>{"id"=>{"6"=>{"approved"=>"0"}, ..

but it complains about not finding a quote without an ID. I don't care about the ID since I want to bulk update all of them. It seems like this particular stucture would map several ids, but maybe (probably) i have to change my controller to compensate for that, not sure how though. It also doesn't work when I try to remove the quotes par tof the hash and just have"id"=>{"6"=>{"approved"=>"0"}, .. as my parameters.

EDIT: I found a statement called update_all, but I don't know how to selectively pass it what I need...

+1  A: 

I found the following approach to be flexible specially for error handling as well:

def bulk_update
  quotes = params["quotes"]["id"]
  quotes.each do |quote_id,quote_attrs|
    quote = Quote.find(quote_id)
    quote.update_attributes(quote_attrs)
    # Other logic of interest like error handling
    # . . .
  end
  # Don't forget to render or redirect to the proper view
end

Update

The proper way to do this is by moving the update logic to the model:

class QuotesController < ApplicationController
  def bulk_update
    errors = Quote.bulk_update(params["quotes"]["id"])
    # Other logic of interest like error handling
    # . . . 
    # Don't forget to render or redirect to the proper view
  end
end

class Quote < ActiveRecord::Base
  def bulk_update
    errors = {}
    quotes.each do |quote_id,quote_attrs|
      quote = Quote.find(quote_id)
      unless quote.update_attributes(quote_attrs)
        errors[quote_id] = qoute.errors
      end 
    end
    errors
  end
end
khelll
I'm getting a nil object for quotes when I try to run it like that. Also, what should my parameters hash be looking like? What variation of: "quotes"=>{"id"=>{"6"=>{"approved"=>"0"}, ..
Stacia
Updated to match your structure, however if it didn't, please submit 2 quotes and paste the resulting structure in params hash.
khelll
I think you should override person.update and do it there, this stuff has no business in the controller.
nasmorn
Yes indeed, it's better that you move this logic to the model after it works for you.
khelll
Perfect, thank you so much. I am still learning rails so I wasn't actually sure what I can pass into the params like that and how the symbol is different than the string type..confusing stuff. Anyway, I'm really confused about what you said about moving it to the model? I didn't think this stuff went in models. Do you mean moving it to the person controller rather than the quotes? Is this because as I said, I don't care about the person it's attached to?
Stacia
Updated it to show the proper way. Usually data manpulation is done in models, your controllers should be skinny.
khelll