views:

27

answers:

3

Hello, sorry for my english,

I have a model named Recipe and Recipe has an attribute named duration

In my form, I have 2 dropdowns to get duration attribute

select_tag 'duration[hours]'
select_tag 'duration[minutes]'

I need the value of duration attribute like this format hh:mm:ss

I have tried with

def duration=(d)
  self.duration = "#{d[:hours]}:#{d[:minutes]}:00"
end

but that is not working

help please

Thk in advice!

A: 

Your form elements are named hours and minutes but your model is looking for h and m keys?

Is that really what you mean?

Gareth
Sorry, I have edited it
el_quick
A: 

Ok, I solved it

select_tag 'custom_duration[hours]'
select_tag 'custom_duration[minutes]'

I created a setter method with a diferent name than attribute to set my attribute

def custom_duration=(d)
  self.duration = "#{d[:hours]}:#{d[:minutes]}:00"
end

thk any way

Regards.

el_quick
+2  A: 

With

def duration=(d)
  self.duration = "#{d[:hours]}:#{d[:minutes]}:00"
end

In your method you call yourself because it's the same method. You need change the attributes duration not the method

def duration=(d)
  write_attribute(:duration, "#{d[:hours]}:#{d[:minutes]}:00")
end
shingara