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.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Mar 9, 8:13 am, John Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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.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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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:
<div class="project">
<% fields_for "receta[project_attributes][]", project do
|project_form| %>
<p>
project: <%= project_form.text_field :name, :index => nil %>
...
</p>
<% end %>
</div>
Second:
So msg = nil unless I pass a parameter msg with some value. It is
correct?
Thanks a lot.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
The code above has an error, but I can not edit it. Job has_many:projects. Not consider receta, it''s ''job''. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 9 Mar 2009, at 10:02, John Smith 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> > Thanks a lot. > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---