Note that there are some explanatory texts on larger screens.

plurals
  1. POrefactoring from controller to model
    primarykey
    data
    text
    <p>I have this action in my Order Controller:</p> <pre><code># POST /orders # POST /orders.json def create @order = Order.new(params[:order]) #spostare il ciclo cart &gt; line_items come metodo del modello Order cart = session[:cart] cart.each do | id, quantity | item = Project.find_by_id(id) @line_item = LineItem.new @line_item.project = item @line_item.quantity = quantity @line_item.price = item.price @order.line_items &lt;&lt; @line_item end @order.user = current_user session.delete(:cart) respond_to do |format| if @order.save #Send an email to admin UserMailer.order_created_to_admin(@order).deliver format.html { redirect_to :dashboard, notice: "L'ordine è stato creato correttamente." } format.json { render json: @order, status: :created, location: @order } else format.html { render action: "new" } format.json { render json: @order.errors, status: :unprocessable_entity } end end end </code></pre> <p>now I want to refactor it a bit, and I'm thinking moving the following lines to Order Model as a method:</p> <pre><code> cart.each do | id, quantity | item = Project.find_by_id(id) @line_item = LineItem.new @line_item.project = item @line_item.quantity = quantity @line_item.price = item.price @order.line_items &lt;&lt; @line_item end </code></pre> <p>like that:</p> <pre><code>class Order &lt; ActiveRecord::Base attr_accessible :user_id belongs_to :user has_many :line_items, :dependent =&gt; :destroy accepts_nested_attributes_for :line_items, :allow_destroy =&gt; true def add_line_items_to_order_from_cart(cart) do cart.each do | id, quantity | item = Project.find_by_id(id) @line_item = LineItem.new @line_item.project = item @line_item.quantity = quantity @line_item.price = item.price line_items &lt;&lt; @line_item end end end </code></pre> <p>and calling it from controller like:</p> <pre><code>order.add_line_items_to_order_from_cart(cart) </code></pre> <p>Does it make sense or not in terms of "skinny controller / fat models" pattern?</p> <p>thank you</p> <p>EDIT</p> <p>My Order model now:</p> <pre><code>class Order &lt; ActiveRecord::Base attr_accessible :user_id belongs_to :user has_many :line_items, :dependent =&gt; :destroy accepts_nested_attributes_for :line_items, :allow_destroy =&gt; true validates :user, :presence =&gt; :true def add_line_items_to_order_from_cart(cart) cart.each do | id, quantity | item = Project.find(id) line_items &lt;&lt; LineItem.new(project: item, quantity: quantity, price: item.price) end end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload