0

enter image description here

I am trying to get this loop to iterate numbers at the end of cardtype. I can't figure out the right syntax. I have tried a bunch of different things.

See red arrow.

4
  • 4
    A good tip to help others help you in the future is to paste your code in the question and the error message you get. Commented Oct 7, 2015 at 19:39
  • 4
    Post code as characters, not as a picture. Commented Oct 7, 2015 at 19:39
  • thanks for the advice Commented Oct 7, 2015 at 19:40
  • 2
    I'm going to doubly complain about the image. Not only is it hard to read, but it's useless when it comes to testing your code. Nobody is going to retype all that to test and repair it. Pasting as clean, formatted code makes it considerably easier to provide a working solution. Commented Oct 7, 2015 at 19:40

2 Answers 2

3

If you really want to do it this way you could use the method send, in this way:

if @order.send("cardtype#{n}") != "none"

This will execute the method "cardtype#{n}" on the @order object.

However, this is generally not the best idea and I would suggest you look into turning this attribute in to a has_many relation or perhaps a serializable array.

Check out this tutorial on how associations work (it is most likely what you want to use): http://guides.rubyonrails.org/association_basics.html

Sign up to request clarification or add additional context in comments.

2 Comments

Agree! You could also iterate with something like ` [1..9].each do ` instead of @numbers, but the "best" way to do this is to make a separate Card model, with the paper weight, types and quantity as fields on the card, and then just do ` @order.cards.each do |card| ` -- it looks like you're learning rails, good luck with it!
I think as of now my understanding is flawed. I am new at rails and right now I am just trying to get this code to email me the details. RIght now the information is coming from a user submitted form and the user can have up to 9 cards. They can only select 1 card if they want to and I only want to. in the email I don't want to show all of the information so I am trying to use this set up. Later once I learn more I want to add the fancier structuring of having users have many cards type setup.
1

As far as I understand, cardtypeN is a method (database column?) where N is an integer. It means there is no easy way to loop over these attributes as for the Ruby virtual machine itself these methods have no connections each other even if you call them cardtype1, cardtype2, etc.

And in fact, your code highlights a very poor design decision to me. If an order can have more card types, you should really h ave a cardtype model and associate order and cardtypes with a 1-to-many relationship.

However, to answer your question, you can dynamically compose the method using one of the following approaches:

# [] syntax
@order["cardtype#{n}"]
# send
@order.send("cardtype#{n}")

Each method has its own advantages and disadvantages. Said that, you can loop from 1 to the number of maximum cardtypes

<% 1.to(10).each do |index| %>
  <% if @order["cardtype#{index}"] != "none" %>
     whatever
  <% end %>
<% end %>

However, as I mentioned, you really want to redesign your model.

4 Comments

The [] notation does read a lot better. It's worth noting that your comments on the poor design decision stem from violating the Zero, One or Infinity Rule of software design. This seems to be a good candidate for a has_many relationship rather than a pile of columns.
@tadman I agree about the has_many, that's why I put some emphasis on it. I really feel that having method names with index values is a terrible approach.
Thanks for the information on the Zero Infinty rule. I will look into the has_many. I think my understanding is just not there yet and I am hacking it together.
I am getting undefined method `[]' for #<Order:0x000001124fa070> I copied your first syntax. @order["cardtype#{n}"]

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.