# Model.create?

**URL:** <https://www.ruby-forum.com/t/model-create/150704>\
**Category:** Rails\
**Created:** [October 11, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/model-create/150704 "2008-10-11T19:59:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [October 11, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/model-create/150704/1 "2008-10-11T19:59:57Z")

</div>

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 _#{@item.inventory.name}_”  
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

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [October 11, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/model-create/150704/2 "2008-10-11T22:05:37Z")

</div>

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 H.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [October 11, 2008, 10:18pm UTC](https://www.ruby-forum.com/t/model-create/150704/3 "2008-10-11T22:18:35Z")

</div>

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
