How can I retrieve information from an active record with collection select? - ruby-on-rails

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.

Related

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

Ruby on Rails - How to use a select box to change an attribute of the selected items?

I'll start off with a bit of context to my question. I have a group of offices which each have reviewers associated with them. A reviewer can only be associated with one office. I want to create two select boxes. One lists all of the reviewers that are associated with the office I am viewing, the other lists all of the reviewers that are available (which is basically all of the reviewers that aren't already assigned to this office).
The goal of the current reviewer's listbox is to set their office to nil when they are selected. The goal of the available reviewers listbox is to set their office to this office's id when they are selected. I'm not sure how to change only the reviewers' office_id when using a select box.
Code-wise, what I have so far is this:
office_controller.rb
def edit
#office = Group.find params[:id] if params[:id]
#current_reviewers = Reviewer.find_all_by_group_id(#office.id)
#available_reviewers = Reviewer.where('group_id <> ?',[#office.id])
end
def update
?
end
office/edit.html.erb
<% form_for(#office, :url => {:controller => :office, :action => :update, :id => #office.id}, :html => {}) do |f| %>
...
<%= select_tag 'removedReviewers', options_from_collection_for_select(#current_reviewers, "id", "display_name"), :multiple => true %>
<%= select_tag 'chosenReviewers', options_from_collection_for_select(#available_reviewers, "id", "display_name"), :multiple => true %>
...
<% end %>
Any suggestions would be greatly appreciated. Thanks!
I solved this by adding the ability to get a select_tag to get the selected objects as a collection via the [] modifier of the select_tag name. So, in this example it was:
<%= select_tag 'removedReviewers[]', options_from_collection_for_select(#current_reviewers, "id", "display_name"), :multiple => true %>
And for the controller code to handle it, I did this:
#removedReviewers = params[:removedReviewers]
if !#removedReviewers.nil?
#removedReviewers.each do |reviewer|
#reviewer = Reviewer.find(reviewer)
#reviewer.group_id = nil
#reviewer.save
end
end
And the equivalent for the chosen/available reviewers.

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

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.

Active Admin - refresh second drop down based on first drop down, Ruby on Rails

I am using Active Admin Gem on Ruby on Rails. I have a form in which i have selected category and sub category and then accordingly i have to fill the data. So i created two tables in sqlite added in active admin resouce.
Every thing is working fine but the drop down of sub category is not getting filtered based on the category choosen.
I am new to Ruby and RoR too. I don't know how to refresh dropdown of the subcategory after selecting category.
I know i can do it from AJAX and javascript but i dont know where to code for that?
Also, is there any specific filter avaliable in Active Admin which will make it happen without ajax or javascript.
Any ideas or help will be highly appreciated.
i don't know if there is any specific filter avaliable in Active Admin, but i solved it in this 3-steps way (assuming category - is a house, subcategory - is a flat):
1-st step: define helper containing ajax request
(of course, you have to predefine path in routes.rb)
#application_helper.rb
def remote_request(type, path, params={}, target_tag_id)
"$.#{type}('#{path}',
{#{params.collect { |p| "#{p[0]}: #{p[1]}" }.join(", ")}},
function(data) {$('##{target_tag_id}').html(data);}
);"
end
2-nd step: add this method for :onchange action
#admin/inhabitants.rb (DSL with formtastic)
form do |f|
f.inputs do
#...
f.input :house, :input_html => {
:onchange => remote_request(:post, :change_flats, {:house_id=>"$('#house_id').val()"}, :flat_id)
}
f.input :flat
#...
end
end
3-rd step: render result of filtering
(you can render partial instead of :text, I decided leave it in one activeadmin resource file )
controller do
def change_flats
#flats = House.find_by_id(params[:house_id]).try(:flats)
render :text=>view_context.options_from_collection_for_select(#flats, :id, :flat_number)
end
end
I accomplished this as any non-rails developer working on a rails project would - quick and dirty. Here's how:
#...
f.input :user, :input_html => {
:onchange => "
var user = $(this).val();
$('#order_location_id').val(0).find('option').each(function(){
var $option = $(this),
isCorrectUser = ($option.attr('data-user') === user);
$option.prop('disabled',!isCorrectUser);
});
"
}
f.input :location, collection: Location.all.map{ |loc|
[loc.name,loc.id, {"data-user" => loc.user_id}]
}
#...
No AJAX required. Note that this does not remove the unwanted options, it just disables them (sufficient for my scenario). This could easily be made modular with a helper, but I really only needed the functionality once.
For anyone else wrestling with the same problem, look at this railscast
I faced the same problem here
here's how I implemented multiple dynamic select menus in activeadmin:
config/initializers/active_admin.rb
config.register_javascript 'exam_registrations.js.coffee'
app/admin/exam_registrations.rb
form do |f|
f.inputs "Exam Registration Details" do
f.input :user_id, :label => 'Teacher', :as => :select, :collection => User.where(:admin => 'false', :active => true).order(:name), :include_blank => true
f.input :student_id, :hint => 'Students grouped by teacher names', :as => :select, :collection => option_groups_from_collection_for_select(User.where(:admin => false, :active => true).order(:name), :students, :name, :id, :name)
f.input :lesson_id, :hint => 'Lessons grouped by student names', :as => :select, :collection => option_groups_from_collection_for_select(Student.where(:active => true).order(:name), :lessons, :name, :id, :name)
end
f.buttons
end
app/assets/javascripts/exam_registrations.js.coffee
#first menu
jQuery ->
$('#exam_registration_student_id').parent().hide()
students = $('#exam_registration_student_id').html()
$('#exam_registration_user_id').change ->
user = $('#exam_registration_user_id :selected').text()
escaped_user = user.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/#])/g, '\\$1')
options = $(students).filter("optgroup[label='#{escaped_user}']").html()
if options
$('#exam_registration_student_id').html(options)
$('#exam_registration_student_id').parent().show()
else
$('#exam_registration_student_id').empty()
$('#exam_registration_lesson_id').empty()
# second menu
$('#exam_registration_lesson_id').parent().hide()
lessons = $('#exam_registration_lesson_id').html()
$('#exam_registration_student_id').click ->
student = $('#exam_registration_student_id :selected').text()
escaped_student = student.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/#])/g, '\\$1')
options = $(lessons).filter("optgroup[label='#{escaped_student}']").html()
if options
$('#exam_registration_lesson_id').html(options)
$('#exam_registration_lesson_id').parent().show()
else
$('#exam_registration_lesson_id').empty()
restart the server and the menus work!
Now it's possible with this gem https://github.com/holyketzer/activeadmin-ajax_filter, use in you form code like this:
f.input :category_id, as: :select # ...
f.input :subcategory_id, as: :ajax_select, data: {
ajax_search_fields: [:category_id],
search_fields: [:subcategory_atrribute],
url: '/admin/subcategories/filter'
}
And in you subcategory resource page:
ActiveAdmin.register Subcategory do
include ActiveAdmin::AjaxFilter
# ...
end
don't forget to include assets
You can also use activeadmin_addons gem Nested Select
dependent-select could be also a good option

How do I sort an activerecord result set on a i18n translated column?

I have the following line in a view:
<%= f.select(:province_id, options_from_collection_for_select(Province.find(:all, :conditions => { :country_id => #property.country_id }, :order => "provinces.name ASC"), :id, :name) %>
In the province model I have the following:
def name
I18n.t(super)
end
Problem is that the :name field is translated (through the province model) and that the ordering is done by activerecord on the english name. The non-english result set can be wrongly sorted this way. We have a province in Belgium called 'Oost-Vlaanderen'. In english that is 'East-Flanders". Not good for sorting:)
I need something like this, but it does not work:
<%= f.select(:province_id, options_from_collection_for_select(Province.find(:all, :conditions => { :country_id => #property.country_id }, :order => "provinces.I18n.t(name) ASC"), :id, :name) %>
What would be the best approach to solve this?
As you may have noticed, my coding knowledge is very limited, sorry for that.
You need to sort the entries after they have been loaded from the database. This should probably do the trick:
Provinces.find(:all, :conditions => {:country_id => #property.country_id}).sort_by{|p| p.name}

Resources