Forum: Ruby on Rails NewbieQ - Relational tables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Gerard (Guest)
on 2005-12-30 13:01
(Received via mailing list)
Hi all,

I'm going nuts on a very simple issue. I know what I want but don't know
what
to tell rails to do so. I have a very common setup for managing
contacts. A
companies table and a contacts table. As you've guessed it's a one to
many
relationship.
The database is setup with the appropriate foreign keys, the models have
been
generated and I added the "has_many :contacts" and "belongs_to :company"
to
the appropriate models
Having put scaffolds in place and wanting to add a contact, when saving
I get:

ActiveRecord::AssociationTypeMismatch in Work#create
Company expected, got String
(I don't now what "Company" represents in this case (model, column or
what
not)

What I eventually want when adding a contact is having a dropdown list
with
the company names so the relation is established.

I have the agile book, and the example webshop was a very clear. But I
can't
seem to implement this. Are there any easy online step-by-step tutorials
how
to go about this, because I'm losing it.

Thanx a lot!

Regards,

Gerard.
Gerard (Guest)
on 2005-12-30 14:15
(Received via mailing list)
Hi all again,

Finally getting somewhere. The relationship thingy seems to be nailed.
I'm
afraid of working with scaffold, because I can't oversee the impact
worry I
might loose code. So I'll do it the manual way.

Nevertheless do I have a question on the code below. The dropdownlist is
in
place. but now al the fields need to be filled. Since I have 20 fields
for a
contact. Is it possible to generate the label - text_field - label -
text_field - etc. iteration dynamically with a loop. (Seems more
managable
like that).

Here's the catch: The company.id field needs to be ignored because
that's
already in the drop downlist.

<h1>New contact</h1>
<form action="/work/create" method="post">
	<b>Company</b><br/>
	<select name="contact[company_id]">
	<% @companies.each do |company| %>
	  <option value="<%= company.id %>">
	  <%= company.name %>
	  </option>
	<% end %>
	</select>

# From here on
<p><label for="contact_firstname">Firstname</label><br/>
<%= text_field 'contact', 'firstname'  %></p>

<p><label for="contact_lastname">Lastname</label><br/>
<%= text_field 'contact', 'lastname'  %></p>

.....
# And another 18 entries like the two up here.
</form>

Thanx a lot.

Gerard -hopefully-no-to-demanding- Petersen

On Friday 30 December 2005 11:59, Gerard tried to type something like:
> get:
> can't seem to implement this. Are there any easy online step-by-step
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails

--
"Who cares if it doesn't do anything?  It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."

My $Grtz =~ Gerard;
~
:wq!
Mathieu C. (Guest)
on 2005-12-30 15:10
(Received via mailing list)
> Having put scaffolds in place and wanting to add a contact, when saving I get:
>
> ActiveRecord::AssociationTypeMismatch in Work#create
> Company expected, got String
> (I don't now what "Company" represents in this case (model, column or what
> not)

Scaffold generator works only with _one_ table. It's strange you got
such message about Compagny that sit on another table.

Please post back your models, and generated controllers & view.
attachments are prefered.
Mathieu C. (Guest)
on 2005-12-30 15:16
(Received via mailing list)
> Finally getting somewhere. The relationship thingy seems to be nailed. I'm
> afraid of working with scaffold, because I can't oversee the impact worry I
> might loose code. So I'll do it the manual way.

There's also a manual way but using the right Helpers :

http://api.rubyonrails.com/classes/ActionView/Help...
This topic is locked and can not be replied to.