f.association what is equivalent in rails when not using simple_form?

2,151 views
Skip to first unread message

simple_n00b

unread,
May 19, 2011, 4:21:17 PM5/19/11
to SimpleForm
I am trying to get a better understanding of how the same forms are
created with and without simple form.
I am familiar with how to get a selection of parents in the child
form, but would like to figure out the "plain rails equivalent" to
this. Here is the simple form example:

class Found < ActiveRecord::Base
has_many :looks
end

class Look < ActiveRecord::Base
belongs_to :found
end

<%= simple_form_for(@look) do |f| %>

<%= f.input :name %>

<%= f.input :number %>

<%= f.association :found, :as => :radio, :label_method => :found_name
%>

<%= f.button :submit %>

<% end %>

Any help on how this association would work without simple_form would
be appreciated. thanks in advance

Carlos Antonio da Silva

unread,
May 19, 2011, 4:42:28 PM5/19/11
to plataformate...@googlegroups.com
f.association basically maps to collection_select, so you can read some docs / guides and use it instead.
--
At.
Carlos A. da Silva

simple_n00b

unread,
May 20, 2011, 9:58:38 AM5/20/11
to SimpleForm
Thanks Carlos!
One more question. I have typically seen f.association used in a child
form to get a selection of potential parents that the child could be
assigned to. This makes sense since the child holds the foreign key of
the parent, but is it possible to do the other way, with a parent form
displaying a collection of potential children that could be assigned
to it?

On May 19, 4:42 pm, Carlos Antonio da Silva

Carlos Antonio da Silva

unread,
May 20, 2011, 10:31:07 AM5/20/11
to plataformate...@googlegroups.com
Yes, it is, we usually make use of a collection of check boxes to mark which possible records should be marker as "child" for the parent record, I guess it's the easy way. SimpleForm handles that when you call f.association :foo, :collection => @foos, :as => :check_boxes, by generating a bunch of check boxes for each item in the collection you give.

simple_n00b

unread,
May 20, 2011, 4:00:44 PM5/20/11
to SimpleForm
Thanks, sorry for getting off the topic of simple_form, but how does
something like that work in collection_select? I am looking at this
example
http://www.railsrocket.com/easy-form-drop-downs-with-collection_select
and wondering in particular what the second parameter would be if you
were selecting children from a parent
collection_select("sales_agent", "customer_id", @customers, "id",
"name")
Because there probably wouldn't normally be a foreign key in the
parent model, right?

On May 20, 10:31 am, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> Yes, it is, we usually make use of a collection of check boxes to mark which
> possible records should be marker as "child" for the parent record, I guess
> it's the easy way. SimpleForm handles that when you call f.association :foo,
> :collection => @foos, :as => :check_boxes, by generating a bunch of check
> boxes for each item in the collection you give.
>

Carlos Antonio da Silva

unread,
May 20, 2011, 7:45:57 PM5/20/11
to plataformate...@googlegroups.com
What you can do is to use the collection with _ids there, like:

  collection_select "post", "category_ids", @categories, :id, :name, :multiple => true

The :multiple option allows you to select multiple values in a select box, by using Control (or sth like that). You can do some research on that in the web related to "html select multiple".

This will allow you to select more "categories", which will be saved as "category_ids" for the given post. 
Not sure if it's that what you meant, but hope that helps.

simple_n00b

unread,
May 23, 2011, 8:12:11 AM5/23/11
to SimpleForm
Thanks again for your time. But not what I meant, in that in your
example post is a child of categories. What I a m trying to figure out
how to do (and not sure if it's possible) would be to do this from the
parent side to select a child. For example using your example above
something like
(in the category form)
collection_select "category", "post_ids", @posts, :id, :name,
:multiple => true

The use for something like this would be if for example I wished to
create a new category and on the same page assign some previously
existing posts to it.

The problem with this is that the category model presumable would not
contain a post_id (foreign key) but rather post would contain a
category_id because post belongs_to categories. That's why I question
if this is possible in a one to many relationship, or if this would
require it to be many to many so that there would be a post_ids on the
join table that could be referenced from the category table.

thanks for your time.


On May 20, 7:45 pm, Carlos Antonio da Silva

Carlos Antonio da Silva

unread,
May 23, 2011, 6:47:02 PM5/23/11
to plataformate...@googlegroups.com
In my example I meant to say that a @post has many @categories, so you should be able to select multiple categories in the multiple select box, and save them overriding the "@post.category_ids" array of category ids. I believe that should work for what you intend to do, if I got it right.
Reply all
Reply to author
Forward
0 new messages