1

So I'm implementing my first many-to-many relationship and I'm having some trouble getting the related collection to populate in my view

For example: simple blog w/ posts and tags

my post controller new action has this var

@tags = Tag.all

next inside my view I have the following loop

<% for tag in @tags %>
    <input type="checkbox" id="<%=tag.id%>" value="<%=tag.id %>"><%=tag.description%>
<% end %>

In the MySQL database I have 2 tags listed but for some reason in the view they don't show (not even a single checkbox input so I assume my loop syntax is invalid)

Anything else I missed here?

2
  • 1
    The loop seems to be valid. Try output some text without tags in your loop. Commented Sep 8, 2010 at 0:40
  • 1
    throw a <%= debug tag%> inside the iterator. I also suggest using @tags.each do |tag| as its more conventional. Commented Sep 8, 2010 at 6:00

1 Answer 1

3

Might be a problem with the new Active Record in Rails 3. Try:

<% @tags.each do |tag| %>

It may look like it's just another method of looping over elements, but I've already had a few issues with Ruby acting on the ActiveQuery object instead of the desired query results.

1
  • This appears to work but it turns out the initial for loop actually worked after a clean db:create/db:migrate so I'm sure this was my fault - thanks for the help! Commented Sep 8, 2010 at 14:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.