# How to write the code in model

**URL:** <https://www.ruby-forum.com/t/how-to-write-the-code-in-model/143505>\
**Category:** Rails\
**Created:** [July 19, 2008, 5:07pm UTC](https://www.ruby-forum.com/t/how-to-write-the-code-in-model/143505 "2008-07-19T17:07:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lostwarrior](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@lostwarrior](https://www.ruby-forum.com/u/lostwarrior)\
**Post date:** [July 19, 2008, 5:07pm UTC](https://www.ruby-forum.com/t/how-to-write-the-code-in-model/143505/1 "2008-07-19T17:07:14Z")

</div>

```
hi i am a beginner for rails.how to write this in a model and how

```

to call in a controller  
def upload  
@image=Image.new  
@image.filename=params[:image][:uploaded\_data].original\_filename  
@image.content\_type=params[:image][:uploaded\_data].content\_type  
@image.siz=params[:image][:uploaded\_data].size  
@image.user\_id=params[:image][:patient\_id]  
@image.save  
end

---

<div class="post-metadata">

**Author:** ![lostwarrior](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@lostwarrior](https://www.ruby-forum.com/u/lostwarrior)\
**Post date:** [July 19, 2008, 9:39pm UTC](https://www.ruby-forum.com/t/how-to-write-the-code-in-model/143505/2 "2008-07-19T21:39:49Z")

</div>

Lost W. wrote:

> ```
> hi i am a beginner for rails.how to write this in a model and how
> 
> ```
> 
> to call in a controller  
> def upload  
> @image=Image.new  
> @image.filename=params[:image][:uploaded\_data].original\_filename  
> @image.content\_type=params[:image][:uploaded\_data].content\_type  
> @image.siz=params[:image][:uploaded\_data].size  
> @image.user\_id=params[:image][:patient\_id]  
> @image.save  
> end

hmm  
perhaps just this…

controller:

def upload  
image = Image.new(params[:image])  
if image.save  
…  
else  
…  
end  
end

model:

def uploaded\_data=(file)  
self.filename = file.original\_filename  
self.content\_type = file.content\_type  
self.size = file.size  
end  
…

MAtthewrudy
