I don't know for sure because I haven't encountered a situation where I'd
need to do things like this... But I'll give it a shot - I'm open to
corrections..

class LineItem < ActiveRecord::Base
     belongs_to :order
     belongs_to :product

     def self.from_cart_item(cart_item)
         li = new
         li.product = cart_item.product
         li.quantity = cart_item.quantity
         li.total_price = cart_item.price
         return li
     end
end

You'd simply use the new method without the self because self is used to
refer to the current instance of the object. Also, you should put return li
at the end; you don't have to, but it just makes the code easier to read.


On Wed, May 5, 2010 at 3:21 PM, dev <devin.corm...@gmail.com> wrote:

> For some reason I am tripping up on the use of self and new.
>
> class LineItem < ActiveRecord::Base
>  belongs_to :order
>  belongs_to :product
>
>  def self.from_cart_item(cart_item)
>    li = self.new
>    li.product = cart_item.product
>    li.quantity = cart_item.quantity
>    li.total_price = cart_item.price
>    li
>  end
> end
>
> --
> 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-t...@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-talk+unsubscr...@googlegroups.com<rubyonrails-talk%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>


-- 
_________________________________

Joshua S. Martin


CONFIDENTIALITY NOTE: This e-mail message, including any attachment(s),
contains information that may be confidential, protected by the attorney
client or other legal privileges, and or proprietary non public information.
If you are not an intended recipient of this message or an authorized
assistant to an intended recipient, please notify the sender by replying to
this message and then delete it from your system. Use, dissemination,
distribution, or reproduction of this message and or any of its attachments
(if any) by unintended recipients is not authorized and may be unlawful.

-- 
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-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to