Forum: Ruby on Rails Model.create ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf8869b3a1268d56386221c7bcded3a4?d=identicon&s=25 Chris Hickman (hickman)
on 2008-10-11 19:59
I have two models (Transaction and RentedItem).

Transaction.rb
  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 => inventory.id,
                               :amount => 1,
                               :price => inventory.price)
    else
      ri = items.first
      ri.update_attribute(:amount, ri.amount + 1)
    end
    ri
  end

Controller

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

Problem
 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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-11 22: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)

Fred

Sent from my iPhone

On 11 Oct 2008, at 18:59, Chris Hickman
<rails-mailing-list@andreas-s.net
Cf8869b3a1268d56386221c7bcded3a4?d=identicon&s=25 Chris Hickman (hickman)
on 2008-10-11 22: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,

Chris
This topic is locked and can not be replied to.