0

I have an array in my ruby like the following:

[#<Order::List code: 1511, Reference: "FRIA004", valuation: nil, full_Address: "1, abc road, xyz", reason_available: "Y", list: "1 : Meter cupboard generic throughout - Very low">,
 #<Order::List code: 1512, Reference: "FRIA005", valuation: nil, full_Address: "2, abc road, xyz", reason_available: "Y", list: "2 : Meter cupboard generic throughout - Very high, 3 : Meter cupboard generic throughout - Very low">,
 #<Order::List code: 1513, Reference: "FRIA006", valuation: nil, full_Address: "3, abc road, xyz", reason_available: "Y", list: "15 : Meter cupboard generic throughout - Low">,
 #<Order::List code: 1514, Reference: "FRIA007", valuation: nil, full_Address: "6, abc road, xyz", reason_available: "Y", list: "16 : Meter cupboard generic throughout - High">]

Model:

class Order::List < ActiveRecord::Base
  self.primary_key = :Code

  def self.display_details(codes)
    codes.each { |x|
      details = Order::List.where(Code: x).first
      reference = details.Reference
      address = details.full_Address
      list = details.list

      display_details = reference + "\n" + address + "\n" + list
      return display_details
  end
end

View:

So I can display the detail list into the textarea like the following:

<%= f.text_area :detail_list, rows: 8, value: (Order::List.display_details(codes)) %>

Output:

FRIA004
1, abc road, xyz
2 : Meter cupboard generic throughout - Very high, 3 : Meter cupboard generic throughout - Very low

Questions:

1) It has been stopped after return the first code. I am using each and want to display each of the code into the text_area

2) How can I display the list(list is the column here) into the new line after the comma?

So my expected output is

FRIA004
1, abc road, xyz
1 : Meter cupboard generic throughout - Very low

FRIA005
2, abc road, xyz
2 : Meter cupboard generic throughout - Very high
3 : Meter cupboard generic throughout - Very low

3 Answers 3

1

ad 1: return ends processing of the method and returns the value. So you should do something like:

def self.display_details(codes)
  retval = ""
  codes.each do |x| 
    .......
    display_details = reference + "\n" + address + "\n" + list + '\n'
    .......
    retval << display_details
  end
  return retval
end

ad 2: Just add \n to the end of every line:

display_details = reference + "\n" + address + "\n" + list + '\n'
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. How can i make the list into the new line. Please have a look at my expected output.
Well, you can collect and return the string, not the list. I will edit my answer
Hi, can you give me some example to collect and return the string please?
Sorry, I have asked to return the list value into new lines. E.g: 1: Meter cupboard very low, 2: Meter cupboard very high into new lines?
1
codes.map do |x|
  details   = Order::List.where(Code: x).first
  reference = details.Reference
  address   = details.full_Address
  list      = details.list

  "#{reference}\n#{address}\n#{list}\n"
end

By the way, it is not your model responsibility to present data. Should be done in a view partial or a presenter.

1 Comment

Hi, Thanks. How can i display the each of the list separated by comma into the newline?
0

I think you should use map or collect instead of each.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.