# Help with this syntaxis

**URL:** <https://www.ruby-forum.com/t/help-with-this-syntaxis/162364>\
**Category:** Rails\
**Created:** [March 9, 2009, 9:13am UTC](https://www.ruby-forum.com/t/help-with-this-syntaxis/162364 "2009-03-09T09:13:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 9, 2009, 9:13am UTC](https://www.ruby-forum.com/t/help-with-this-syntaxis/162364/1 "2009-03-09T09:13:08Z")

</div>

I have some doubts when to use this syntaxis in models:

First, width this code:  
def project\_attributes=(project\_attributes)  
…  
end

And second width this code:  
def add\_to\_cart(msg = nil)  
…  
end

I don’t know when should I use this syntaxis in this model methods. Can  
someone explain it with small examples. I’ll appreciate any help.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 9, 2009, 10:29am UTC](https://www.ruby-forum.com/t/help-with-this-syntaxis/162364/2 "2009-03-09T10:29:17Z")

</div>

On Mar 9, 8:13 am, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end
> 
> I don’t know when should I use this syntaxis in this model methods. Can  
> someone explain it with small examples. I’ll appreciate any help.

Well at a basic level the first defines a method called  
project\_attributes= (so that you can do some\_object.project\_attributes  
= something) and the other defines an add\_to\_cart method with an  
optional argument that defaults to nil). Not much more that can be  
said without some context.

Fred

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 9, 2009, 11:02am UTC](https://www.ruby-forum.com/t/help-with-this-syntaxis/162364/3 "2009-03-09T11:02:03Z")

</div>

Some more code:

First example:  
Model  
def project\_attributes=(project\_attributes)  
project\_attributes.each do |attributes|  
if attributes[:id].blank?  
projects.build(attributes)  
else  
project = projects.detect { |t| t.id == attributes[:id].to\_i }  
project.attributes = attributes  
end  
end  
end

Controller:  
def new  
…  
3.times{ @job.projects.build }  
end

View:

 \<% fields\_for "receta[project\_attributes][]", project do |project\_form| %\> 

project: \<%= project\_form.text\_field :name, :index =\> nil %\> ...

 \<% end %\> 

Second:  
So msg = nil unless I pass a parameter msg with some value. It is  
correct?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 9, 2009, 2:40pm UTC](https://www.ruby-forum.com/t/help-with-this-syntaxis/162364/4 "2009-03-09T14:40:51Z")

</div>

On 9 Mar 2009, at 10:02, John S. wrote:

> Second:  
> So msg = nil unless I pass a parameter msg with some value. It is  
> correct?

yes, that’s what optional arguments do.

Fred

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 9, 2009, 11:03am UTC](https://www.ruby-forum.com/t/help-with-this-syntaxis/162364/5 "2009-03-09T11:03:37Z")

</div>

The code above has an error, but I can not edit it. Job  
has\_many:projects. Not consider receta, it’s ‘job’.
