Dr.Gonzo
Dr.Gonzo

Reputation: 107

Selecting specific attributes in Rails 5

From the Hash below titled 'mambu_repayments' I am trying to iterate over it and only select the repayments that have a state value of anything but GRACE. I run the code and yet everything remains the same all fields are appearing on my view with GRACE and other states

for repayment in mambu_repayments.select {[:state] != 'GRACE'} do
// some code here
end

Any help would be great, my syntax needs tweaking..

Upvotes: 0

Views: 51

Answers (4)

Przemek Mroczek
Przemek Mroczek

Reputation: 357

Ok, I am guessing you are using some kind of collection for payments. This is my answer and you could easily check it from irb.

mambu_repayments = [ { name: 'awesome', state: 'DIFFERENT'}, { name: 'great', state: 'GRACE'} ]

mambu_repayments.select { |payment| payment[:state] != 'GRACE' }.each do |payment|
  puts payment
end

Upvotes: 0

d1ceward
d1ceward

Reputation: 1035

'for' are ugly in oriented object language, it do the trick with one more line but consume less memory

mambu_repayments.each do |mambu_repayment|
  # Skip GRACE
  next if mambu_repayment[:state] == 'GRACE'

  # Your view code
end 

Upvotes: 1

Dr.Gonzo
Dr.Gonzo

Reputation: 107

Based off Erez Rabih's answer.. the below worked..

for repayment in mambu_repayments.select  {|s| s["state"] != 'GRACE'} do

end

Upvotes: 0

Erez Rabih
Erez Rabih

Reputation: 15788

try that:

for repayment in mambu_repayments.select {|o| o[:state] != 'GRACE'} do
  // some code here
end

Upvotes: 1

Related Questions