Setting validations on multiple attributes: A noobies attempt

Hi there,

I was trying to be a little creative and play around with ruby. In my
RoR model file I wrote this:

Hash[‘title’, ‘teaser’].each do |k ,v|
validates v.to_sym :presence => true
end

It’s fairly obvious what I’m trying to achieve here, but the validation
is only set for the last element, ‘teaser’. I know there is something
about ruby block only returning the last line (or something), but is
there a way to circumvent that?

Regards,

Nadeem

On Tue, Dec 6, 2011 at 9:25 PM, Nadeem Q. [email protected]
wrote:

Hi there,

I was trying to be a little creative and play around with ruby. In my
RoR model file I wrote this:

Hash[‘title’, ‘teaser’].each do |k ,v|
validates v.to_sym :presence => true
end

Hash[‘title’, ‘teaser’] creates a hash with one pair. Inside the block,
k is assigned ‘title’ and v is assigned ‘teaser’. try

[‘title’, ‘teaser’].each do |k|
validates k.to_sym, :presence => true
end

Posted via http://www.ruby-forum.com/.