Sunday, May 8, 2011

[Rails] Re: Beginners question | moving a method from controller to model

Jeffrey L. Taylor wrote in post #997405:
> See inserted comments below.
>
> Quoting linda Keating <lists@ruby-forum.com>:
>> new_capacity = event.capacity
>> format.xml { render :xml => @line_item, :status => :created,
>>
>>
> new_capacity and event are local variables in the controller method.
> They are
> not accessible in the called model method. @line_item is an instance
> variable
> of the controller instance. It is also not accessible in the model
> method.
> However it references the same object as self in the model method.
> Presumably, event is another model. Note: it is possible that a
> LineItem has
> an event method because there is an association between Events and
> LineItems.
>
> I don't see any good reason to push this functionality into a model
> method.
> The only property of the model that is clearly being used is the
> quantity
> attribute/method. Unless the LineItem model includes a reference to an
> Event
> (i.e. LineItem belongs to an Event or hasMany Events). It still looks
> like
> poor design unless there are several factors not mentioned here.
>

Hi Jeffrey, I was moving it into the model soley because I've read that
fat models and skinny controllers are the best design practice. I was
just trying to get the function to work first, and now that it's working
I was hoping to tidy up the design. But maybe I should just leave it as
is. I have though similar functionality in the update controller that
looks like this ...


def update
@line_item = LineItem.find(params[:id])
event = @line_item.event
new_quantity = params[:line_item_quantity].to_i
change_in_capacity = @line_item.quantity - new_quantity

respond_to do |format|
if @line_item.update_attributes(params[:line_item])
if (@line_item.quantity >0)
event.capacity = change_in_capacity
event.save


and I'll be writing something similar again reversing the action in the
destroy method of line_item.

But maybe I should just leave it as is for the moment until I've learned
a bit more about Ruby and Rails.


Many thanks for your help and feedback. Really appreciate your
patience.

Linda

> Jeffrey
>
>> Rails.root: c:/webtech/ServerSide/tickets_online
>>
> It isn't quantity that isn't working, it is that @line_item is nil
> (i.e.,
> @line_item of the LineItem instance has not been assigned a value).
> @line_item is an instance variable in the controller, not in the model.
>
>> I really am an absolute beginner so I'm really just guessing most of the
>> time. Any suggestions or hints would be most appreciated.
>
> HTH,
> Jeffrey

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate