-1

I am making an app in Rails 4. I use Simple Form for forms.

I have a profile model and an organisation model.

The associations are:

profile.rb

 has_one :organisation, as: :orgable

organisation.rb

has_many :profiles

In my organisation table, I have an attribute called :org_type.

In my organisation form, I ask users to select from an array of types of organisation:

<%= f.input :org_type, :label => "Organisation type", collection: [ "University", "Research Organisation", "Company"] %>

In my profile form, I want to ask users which uni they study at.

I want to use the array of universities created within the organisation model.

I have a scope in my organisation model to filter out the universities:

scope :all_uni, -> { where(org_type: 'University') }

In my profile form I have:

<%= f.input :institution, :label => "Graduated from" %> 

But this just has a text field.

I have tried to replace that line with an attempt at making a select function in my form which refers to my organisation model scope for all_uni. It looks like this:

<%= f.select(:institution, @organisation.all_uni.title.map { |value| [ value, value ] }) %>

It gives this error:

undefined method `all_uni' for nil:NilClass

I don't understand what this error message means, but I'm also not sure I'm on the right track with the form select field either. Any tips for where to look to get this working. I'm not sure how to go about setting up the select field in the first place?

ANOTHER ATTEMPT:

I have also tried using this in my profile form:

<%= f.select(:institution, @organisation.all_uni.title) %>

But I get the same error. I must be way off track - i've exhausted every option I can find.

ANOTHER ATTEMPT

I found this post Rails Simple Form custom association select field

Taking the example in that solution, I tried:

<%= f.input :institution do %>
                    <%= f.select :institution, Profile.Organisation.all_uni.map{ |l| [l.title  {:title => l.title.titlecase}] } %>
                    <% end %>

But, I get this syntax error. I've tried removing the => but keep getting more syntax errors.

syntax error, unexpected =>, expecting '}' ...i.map{ |l| [l.title {:title => l.title.titlecase}] } );@out... ... ^
Community
  • 1
  • 1
Mel
  • 2,481
  • 26
  • 113
  • 273

1 Answers1

0

Not a complete answer but according to what I know is, If you got 2 models then instead of using

profile.rb
 has_one :organisation, as: :orgable

organisation.rb
 has_many :profiles 

You can simply use

profile.rb    
 belongs_to :organisation

organisation.rb    
 has_many :profiles
Malware Skiddie
  • 318
  • 2
  • 12
  • Hi Malware. Thanks. The extra bit is required where the model is polymorphic. Thanks very much anyway. – Mel Dec 30 '15 at 07:10