Collection select with many-to-many in same model in rails gives error - ruby-on-rails

I have a weird rails database/form problem.
I got 1 table with Courses, and 1 table Prerequisites, which has 2 columns that both hold Course_ids (columns are called "course_a_id" and "course_b_id").
In the Course model I have this:
:has_and_belongs_to_many(:prerequisites,
:join_table => "prerequisites",
:foreign_key => "course_a_id",
:association_foreign_key => "course_b_id",
:class_name => "Course")
From this SO answer
This works if I put the prerequisites in by Console, something like this:
Course.find(3).prerequisites = [Course.find(1), Course.find(2)]
This form field however does not let me put prereqs in the DB:
<%= f.label :prerequisite, "Prerequisites" %>
<%= f.collection_select(:prerequisites, Course.all, :id, :name,
{:multiple => true}, :multiple => "multiple") %>
Gives this error after posting with one option selected with value 1:
"Course(#-631146998) expected, got String(#77208170)"
and in the parameter dump:
"prerequisites"=>["",
"1"]}
I have no clue how that "" ended up in the params and can't figure out another way to create the form field. I think my model is set up correctly.

Related

Rails. Has_many :through and form_for params for a checkbox field

So I have this kind of association:
class FirstModel
has_many :merged_models
has_many :second_models, :through => :merged_models
end
class SecondModel
has_many :merged_models
has_many :first_models, :through => :merged_models
end
class MergedModel
belongs_to :first_model
belongs_to :second_model
end
Now my problem is to understand this trick that helps check_box_tag helper to recognise elements in HTML from a passed collection in my form:
form_for(first_model) do |f|
<% SecondModel.all.each do |s| -%>
<div>
<%= check_box_tag 'second_model_ids[]', s.id, first_model.second_models.include?(s), :name => 'first_model[second_model_ids][]'-%>
<%= label_tag :second_model_ids, s.first_name -%>
</div>
<% end -%>
What I do not understand is this:
first_model.second_models.include?(s), :name => 'first_model[second_model_ids][]'
I believe that this:
first_model.second_models.include?(s)
checks if SecondModel's object id is already in FirstModel's second_model_ids array. In this case I would expect something like an if statement - if this id is there then do that, etc.
And this part makes me even more confused:
:name => 'first_model[second_model_ids][]'
Where that :name came from? Why first_model[second_model_ids][] have two square brackets - how they work in Rails syntax? To merge this newly checked id to the second_model_ids array?
I will appreciate all info. Thanks!
So check_box_tag has this signature:
check_box_tag(name, value = "1", checked = false, options = {})
In your case:
check_box_tag 'second_model_ids[]', s.id, first_model.second_models.include?(s), :name => 'first_model[second_model_ids][]'
The first parameter (name) is 'second_model_ids[]', this will be used as the id= part of the tag.
The second parameter (value) of the checkbox is the id of s (current instance of SecondModel).
The third parameter (checked) is:
first_model.second_models.include?(s)
You are right about the meaning, and you don't need an 'if'. The include?() returns a boolean (like most Ruby methods that end in a question mark). You can try this in irb or rails console:
[1,2,3].include?(2)
# => true
The final option:
:name => 'first_model[second_model_ids][]'
passes in a hash of options which will be used as html. In this case a single hash value with the key :name (not to be confused with the first parameter above, which was used as the id='...' in the html tag), this will be used directly in the tag as
name='first_model[second_model_ids][]'
You were right about the syntax here also. The brackets help Rails parse this into the correct nesting of the params hash with
first_model: {foo: 1, bar: 2, second_model: {some: stuff, other: stuff}}

ROR HABTM implementation with simple forms

I am trying to implement HABTM model with two of my database tables. Two tables are Users and Ratings. According to general structure I should have following things
Users Model/Table
Ratings Model/Table
User_ratings Table
But User_ratings will have an extra column other than two ids and that will be a pre-defined string named stage. Stage will have pre-defined values such as appointment, completion e.t.c.
My first question is does this change the role of my middle table (through table) or will we still create just a migration for middle table.
Secondly, I am trying to implement a simple form to assign different users different ratings and my code looks like.
= simple_form_for #appointment, :html => {:class => 'form-horizontal'} do |f|
= f.input :start_datetime, as: :datetime_picker, input_html: {class: "datetime form-control"}
= f.input :end_datetime, as: :datetime_picker, input_html: {class:"datetime form-control"}
/ Now here I want three fields belonging to my middle table. Something like
= r.input :contact_id, :collection => Contact.all, :label_method => :get_contacts
= r.inout :rating_id, :collection => Rating.all, :label_method => :get_ratings
= r.hidden_field :stage, value: 'Appointment'
The problem here is that I don't have a model for middle table and hence I am stuck with how to do it with simple_form
Any kind of help will be highly appreciated. Thanks

Rails Simple Form Select from Array - polymorphic associations

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... ... ^
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

Set multiple attributes on basis of results of a :select in rails form

I have a select box on my form which retreives an object with the following attributes:
:id, :v2_read_code, :v2_term
The select code is:
f.inputs "Tests" do
f.has_many :eqa_material_tests, :allow_destroy => true, :heading => 'Tests In EQA Material' do |cf|
cf.input :test_id, :as => :select, :collection => Hash[Test.all.order(default: :asc).map{|b| [b.v2_term,b.id]}]
end
end
Where I am storing the test id in a model/table with the following structure:
eqa_material_tests
id, test_id, eqa_material_id
In addition to storing the test_id, I'd also like to store the v2_read_code and v2_term as I'd like to keep a copy of these items if possible.
Is this possible?
After a nights sleep I've realised I'm approaching this wrong. I can do this using an active record callback like after_create

How can I retrieve information from an active record with collection select?

I have 3 models (Users - Membership - Community)
The users can become members to many communities. For this I made a Membership that contain the user_id, community_id.
After connected, the user have to choose a community. The model User as a community_id that contain that unique community.
When editing, he would be able to change this community.
If I do this :
<%= f.collection_select :community_id, Community.find(:all), :id, :name, { :allow_blank => 'Select a community' }, :style => "width: 200px;" %>
All the communities happier, also that who he is not member.
I tried this :
<%= f.collection_select :community_id, Membership.find(:all), :community_id, :id, { :allow_blank => 'Select a community' }, :style => "width: 200px;" %>
But I show only the number (:id) of the Membership…
How can I join this id with the name of the community ?
Not sure if this will work but try it out:
member.rb # add a method to the member model that returns the
def community_name
community.name
end
#view
<%= f.collection_select :community_id, Membership.find(:all, :include => :community), :community_id, :community_name, { :allow_blank => 'Select a community' } %>
The :include option prefetches all communities in the membership collection in one query.
I think you were closer with your first attempt but instead of finding all communities you need to just find communities that the user is a member of. So instead of Community.find(:all) you would use:
Community.find(:all,
:includes => :memberships,
:conditions => ['memberships.user_id = ?', #user.id])
This assumes that you have an #user variable set up for your view. You need this to restrict the find to just communities that your user is a member of.
It also assumes that there's an association on Community: has_many :memberships. I've guessed you've got that already from the question.

Resources