Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

Newbie question trying to understand collection_check_boxes method

Rails • Asked by MH

Hi, I am new to Rails, and I am trying to create checkboxes in a view. I came across this method:

What I am trying to understand here is, where does this “:author_ids” method come from? Is this similar to author.ids method? I have been reading through the guide but struggling to find out where is the method coming from? Also I do not quite understand the purpose of parsing this “:name_with_initial” into the method.

Appreciate for any advice/explanation on this.

That would come from a has_may :authors association. ActiveRecord knows how to handle those associations through those methods.

It's documented here as collection_singular_ids

As I understand it, author_ids is one of those Rails built-in magic merhods that gives you all the IDs of associated records. For example, if I have a School that has_many :teachers I can do School.find(1).teacher_ids to get all the IDs of all the teachers associated with that school. Super handy and saves writing some gnarly SQL.

The name_with_initial method is generating the text shown alongside the checkbox. It could just be a plain text column from Author, like first_name but this is showing you how you can spruce it up a bit and use a method to generate fotmatted text instead.

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 31,152+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.