I have a form with two collection_select fields, the first one is an easy one, it just gets a model named courses, which shows the course name, and of course returns the id of the selected course, the second is the one Im having problems with, its a collection_select of the similar courses a course may have.
The courses model:
class Course < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
attr_accessible :code, :credits, :name, :description, :active
has_many :similars, dependent: :destroy
has_many :similar_courses, through: :similars, source: :similar
end
The similar model:
class Similar < ActiveRecord::Base
attr_accessible :course_id, :similar_id
belongs_to :course
belongs_to :similar, class_name: "Course"
validates :similar_id, presence: true
validates :course_id, presence: true
end
this is the homologate model, the thing with this model is that a course must be approved or rejected if one wants to transfer classes and stuff like that:
class Homologation < ActiveRecord::Base
attr_accessible :homologate_by, :homologate_course, :student_id
belongs_to :user
end
this is the form Im having problems with:
<%= form_for(@homologation) do |f| %>
<%= render 'shared/error_messages', object: @homologation %>
<%= f.label :homologate_course %>
<%= f.collection_select :homologate_course, Course.find(:all), :id, :name, :prompt => "Select a Course" %>
<%= f.label :homologate_by %>
<%= f.collection_select :homologate_by, Similar.find(:all), :similar_id, :name, :prompt => "Select a Similar Course" %>
<div class="form-actions">
<%= f.submit "Create Homologation", class: "btn btn-large btn-primary" %>
</div>
<% end %>
</div>
Im getting the following error
http://dpaste.com/hold/827744/
the Bartolleti thing is the name of the course I want to be able to show, and that of course is not a method, but I dont know Why Im getting the error, I want to be able to show the names of the similar courses given the first collection field course…
Thank you for your help!
From the doc of collection_select :
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})Then with your code you have
Course.find(Similar.find_by_id(:similar_id)).nameas thetext_method, that is why you get this error message.One solution could be to add a method on your
Similar modelto get the similar course name :then you can use it as the
text_methodin yourcollection_select: