# Noob question:controller-model problem

**URL:** <https://www.ruby-forum.com/t/noob-question-controller-model-problem/181108>\
**Category:** Rails\
**Created:** [January 5, 2010, 5:43pm UTC](https://www.ruby-forum.com/t/noob-question-controller-model-problem/181108 "2010-01-05T17:43:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![radu\_puspana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@radu\_puspana](https://www.ruby-forum.com/u/radu_puspana)\
**Post date:** [January 5, 2010, 5:43pm UTC](https://www.ruby-forum.com/t/noob-question-controller-model-problem/181108/1 "2010-01-05T17:43:04Z")

</div>

hi guys, hope u are all out there…

so this is my controller method that creates a new product(and stores  
into the db)  
def create  
vendor\_id = session[:user\_id]  
@product = Product.new(params[:product])  
@product.add\_vendor\_id(vendor\_id)

```
respond_to do |format|
  if @product.save
    flash[:notice] = 'Book entry successfully created.'
    format.html { redirect_to(@product) }
    format.xml { render :xml => @product, :status

```

=\> :created, :location =\> @product }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @product.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

the view : has fields into which the user inserts some values, minus  
his id given by the db, froam table users

the model:  
class Product \< ActiveRecord::Base  
has\_many :line\_items

```
validates_presence_of :title, :description, :image_url
validates_numericality_of :price
validates_uniqueness_of :title
validate :price_must_be_grater_than_zero
validates_format_of :image_url,
                    :with => %r{\.(gif|jpg|png)$}i,
                    :message => 'must be a URL for GIF, JPG or PNG

```

image.’

```
def self.search(search)
 find(:all, :conditions => ['title LIKE ?', "%#{search}%"])
end

def self.find_products_for_sale
    find(:all, :order => "title" )
end

def self.add_vendor_id(vendor_id)
        self.vendor_id = vendor_id
end

protected
    def price_must_be_grater_than_zero
        errors.add(:price, 'should be at least $ 0.01') if

```

price.nil? || price \< 0.01  
end  
end

The problem is that rails will not save the products object giving  
this error :  
NoMethodError in ProductsController#create  
undefined method `add\_vendor\_id’ for #Product:0xb60624ec

Pls help…

a billion thx in advance,  
radu

---

<div class="post-metadata">

**Author:** ![radu\_puspana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@radu\_puspana](https://www.ruby-forum.com/u/radu_puspana)\
**Post date:** [January 5, 2010, 6:12pm UTC](https://www.ruby-forum.com/t/noob-question-controller-model-problem/181108/2 "2010-01-05T18:12:55Z")

</div>

On Jan 5, 11:41Â am, radu puspana [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Â Â def self.add\_vendor\_id(vendor\_id)  
> Â Â Â Â Â Â self.vendor\_id = vendor\_id  
> Â Â end

def \*\*\*self.\*\*\*add\_vendor\_id means add\_vendor\_id makes add\_vendor\_id a  
class method, not an instance method as you want it to be.

---

<div class="post-metadata">

**Author:** ![radu\_puspana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@radu\_puspana](https://www.ruby-forum.com/u/radu_puspana)\
**Post date:** [January 5, 2010, 6:24pm UTC](https://www.ruby-forum.com/t/noob-question-controller-model-problem/181108/3 "2010-01-05T18:24:37Z")

</div>

PROBLEM SOLVED!
