1

As this will be my first Q on StackOverflow ... please be gentle?

When assigning a value to an attribute in Table A (f.e. projects, :project_owner), I want to present a "choose from", consisting of objects from another Table B (f.e. people, :person_name) and commit the chosen one ...

So what would be best practice?

thnx in advance ...

BerryGJS
  • 97
  • 1
  • 13

2 Answers2

0

Welcome to StackOverflow!

One quick side note- Ruby in an object oriented language, so instead of thinking about it from a table perspective, think of it from a class perspective.

That said you should take a look at the documentation for FormHelpers. There's an example for exactly what you're looking for.

Here's what you would want to do in your form_for block:

f.select("people", "people.id", People.all.collect {|p| [ p.name, p.id ] }, { include_blank: true })
RobertH
  • 528
  • 3
  • 13
  • This didn't work for me ... error: "undefined method 'merge' for personxxxxxxx" The concepts of "classes" and "tables" are beginning to work their way in to me ... thnx! – BerryGJS Jan 17 '16 at 17:40
  • Well I had to guess what the names of your classes are. – RobertH Jan 17 '16 at 17:47
  • class: Person / table: people, so Person.all.collect ... but it didn't work. – BerryGJS Jan 17 '16 at 19:10
  • NoMethodError in Projects#new Showing /home/ubuntu/workspace/app/views/projects/_form.html.erb where line #26 raised: undefined method `name' for # Extracted source (around line #26): 24 25 26 27 28 29
    <%= f.select("people", "people.id", Person.all.collect {|p| [ p.name, p.id ] }, { include_blank: true }) %>
    – BerryGJS Jan 17 '16 at 19:24
0

collection_select will help you:

<%= form_for @project do |f| %>
   <%= f.collection_select :owner_id, Person.all, :id, :name %>
   <%= f.submit %>
<% end %>
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • Yes, works! So next Q. -> Will there be an "automatic" association between a person and the projects he/she runs. (:person has_many :projects)? Or is this just a string being passed in? – BerryGJS Jan 17 '16 at 17:48
  • I've called it `owner_id` but it needs to be whatever your `foreign_key` is for your table (`person_id`?). Once created, the association will also be defined as well – Richard Peck Jan 17 '16 at 17:51
  • Let me answer.self -> in the console I see 'person.id' stored in 'consultant'. Meaning I have to make an association between 'project: belongs_to :person (foreign key)' and ' consultant: has_many :projects. – BerryGJS Jan 17 '16 at 17:55
  • What's your foreign key in the `projects` table, is it `person_id`? – Richard Peck Jan 17 '16 at 18:03
  • The foreign_key is stored in a column named :consultant. Seems I'm running into a challenge here ... how to reference multiple persons within one project (e.g. consultant, assistant, manager)? ... maybe like this: http://stackoverflow.com/a/14868254/5799625 – BerryGJS Jan 17 '16 at 19:14
  • One more thing now ... the collection form works, but the choosen option is not committed at submit? Must be a very basic issue, but I can't find the solution. thnx in advance .. – BerryGJS Jan 18 '16 at 21:54
  • Have you permitted the parameter? I don't think you understand `foreign keys`, which would be the primary source of the problem IMO – Richard Peck Jan 18 '16 at 21:58
  • you're right, I had to permit params ... but now I get this one: ActiveRecord::AssociationTypeMismatch in ProjectsController#update Person(#70063006855440) expected, got String(#9070440) Seems obvious, but how to correct this? – BerryGJS Jan 18 '16 at 22:09
  • You're trying to populate `person` it seems; you need to populate `person_id` – Richard Peck Jan 18 '16 at 22:09
  • in my Person model I associate like this (because people are associated in 3 different ways): ` has_many :primary_projects, :class_name => "Project", :foreign_key => "consultant_id" has_many :secondary_projects, :class_name => "Project", :foreign_key => "donor_cp_id" has_many :tertiary_projects, :class_name => "Project", :foreign_key => "beneficiary_cp_id" ` so I'm filling `:consultant_id`: `f.select :consultant_id, Person.all.collect {|p| [ p.full_name, p.id ]} ` – BerryGJS Jan 18 '16 at 22:31
  • Guess I have to dig in polymorfic associations? – BerryGJS Jan 18 '16 at 22:48
  • The model setup looks fine. Polymorphic associations are only if a single association can be used by multiple models. If you have multiple associations, there's absolutely no problem referencing them all. – Richard Peck Jan 19 '16 at 10:26
  • Populating the foreign key `consultant_id` should work fine. The best way to fix this will be to post a new question and link to it on here. If you can do that, I'll happily write a more comprehensive answer for you – Richard Peck Jan 19 '16 at 10:26
  • thnx for helping me out. It finally seems to be working. This article was also very helpful: [http://stackoverflow.com/questions/757512/best-practices-for-multiple-associations-with-the-same-class-in-rails] I understand you want to 'score' answers (-:, but as for now I don't have any Q's. However, I'm sure many many many are to come soon!! thnx again, Berry – BerryGJS Jan 19 '16 at 14:52