Text_field with has_many, :through =>

This should be a really simple thing, I just haven’t figured out how to
do it yet…

I have two models with a has_many, :through => relationship:

class Source < ActiveRecord::Base
has_many :authorships
has_many :authors, :through => :authorships

class Author < ActiveRecord::Base
has_many :authorships
has_many :sources, :through => :authorships

class Authorship < ActiveRecord::Base
belongs_to :source
belongs_to :author

I’m not sure how to create a form for this. My sources/new form has a
nested form for authorship that currently looks like this (I’m following
the instructions Ryan B. gives in Advanced Rails Recipes for
dealing with multiple models in one form):

<% new_or_existing = authorship.new_record? ? ‘new’ : ‘existing’ %>
<% prefix = “source[#{new_or_existing}_authorship_attributes][]” %>

<% fields_for prefix, authorship do |authorship_form| %>

<%= authorship_form.text_field :author_id %> <%= link_to_function "remove", "$(this).up('.authorship').remove()" %>

<% end %>

This works just fine, and saves the data just fine, but obviously I
would like to be able to type in author.name instead of :author_id. I
would also be happy to use collection_select here, but haven’t been able
to figure out how to do that with a has_many, :through => relationship.

Thank you!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs