Forum: Ruby on Rails Model.create ?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Chris H. (Guest)
on 2008-10-11 21:59
I have two models (Transaction and RentedItem).

  has_many :rented_items
  has_many :inventories, :through => :rented_items

  def add(inventory_id)
    items = rented_items.find_all_by_inventory_id(inventory_id)
    inventory = Inventory.find(inventory_id)
    if items.size < 1
      ri = rented_items.create(:inventory_id =>,
                               :amount => 1,
                               :price => inventory.price)
      ri = items.first
      ri.update_attribute(:amount, ri.amount + 1)


def add
    @inventory = Inventory.find(params[:id])
    @item = @transaction.add( <= where @transaction is set
in a  before_filter
    flash[:notice] = "Added <em>#{}</em>"
    redirect_to :controller => "inventory", :action => "list"

 I dont receive any errors and when list in rendered I see the correct
flash[:notice].  In the add method inside Transaction.rb the object is
created and return but when I look in the database no record was saved.
I am completely lost on why the record wont save but is being created.

***I even tried RentedItem.create(...) in the controller to remove all
confusion from the controller to model ... and it still wont work.***

Could someone point me in any direction?  Thanks
Frederick C. (Guest)
on 2008-10-12 00:05
(Received via mailing list)
Having an association called transaction is a bad idea as the accessor
methods generated will overwrite the transaction method (which is used
when saving)


Sent from my iPhone

On 11 Oct 2008, at 18:59, Chris H.
Chris H. (Guest)
on 2008-10-12 00:18
I was just in the middle of renaming everything and creating new models
to see if it would help.  Good to know I am already on the right track.

Thanks a lot,

This topic is locked and can not be replied to.