Help with this syntaxis


#1

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.


#2

On Mar 9, 8:13 am, John S. 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


#3

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.


#4

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


#5

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