How to use `where` condition in options collections select? - ruby-on-rails

How can I use where condition in options select? The following code is not working:
<%= options_from_collection_for_select(#group,:groupname, :groupname).where(User_id: #user.id)%>
Can anyone help on this?

options_from_collection_for_select takes a collection of records and options and returns a HTML string. So that of course won’t work as you are calling .where on a string.
You need to call it on the collection instead.
<%= options_from_collection_for_select(#group.where(User_id: #user.id), :groupname, :groupname) %>

Related

How to display ROR 5 ActiveRecord pluck as String in html.erb

In html.erb I have:
<%= ContactDescribe.where(["contact_describe_id = ?", "12"]).limit(1).pluck(:borrower_or_lender_text) %>
The field is retrieved successfully. But returns an array element. I need to learn how to convert that element to a string.
In addition to Deepak's answer, you can also convert the Array into a "Sentence" String
<%= ContactDescribe.where(contact_describe_id: 12).limit(1).pluck(:borrower_or_lender_text).to_sentence %>
Recommendation:
As pointed out by TheChamp, it is best practice to already "prepare" the values needed in the views as instance variables from the controller. See my recommended refactor
# controller
def YOUR_ACTION_NAME
#contact_describe = ContactDescribe.where(contact_describe_id: 12).first
end
# view
<%= #contact_describe.borrower_or_lender_text %>
Note: you won't even need pluck anymore unless you have other reasons why you want limit(1)
The issue here is that where returns a collection - something similar to an array, just in ActiveRecord - no matter what limit you set on it. To retrieve the information you would use .first or [0] since you always only return one object.
But, since you are looking for a specific ContactDescribe object. Do this instead:
ContactDescribe.find_by(contact_describe_id: 12).borrower_or_lender
Additionally there two things you should improve in your code.
1: Logic should go into the controller or the model. A view is solely here to show objects.
2: What is up with the contact_describe_id field? Why not call it id. It seems redundant. Isn't user.id more convenient than user.user_id?
You can make use of join
<%= ContactDescribe.where(contact_describe_id: 12).limit(1).pluck(:borrower_or_lender_text).join(',') %>

Filtered options for select

I'm really stuck in a issue.
I've a RoR application in which I'd like to populate select_tag with filtered options lets say Physicians. Here is my select_tag in view:
<%= select_tag "phyID",options_from_collection_for_select(#physicians,'id','fullNamePhy'),:include_blank => true %>
In controller I have
#physicians=User.find_by_userType('Physician')
but I'm getting error:
undefined method `map' for #<User:0x3b09820>
It seems like I have to use User.all instead of User.find. Please let me know any work around. Thanks in advance
This should work:
#physicians = User.where(userType: 'Physician')
You're getting error because options_from_collection_for_select expects object that behaves like Array, for example ActiveRecord::Relation instance. But find_by_* dynamic finder returns object representing singular record, User instance in this case.
BTW, column names in Rails are by convention named with underscore instead of camel case , like user_type.
You can use dynamic find_all_by finder
#physicians = User.find_all_by_userType('Physician')
find_by_column_names returns single record. Where as select_tag expects collection/array of record

How to get each element of a Rails list into select_tag in a form?

I have a rails list like this :
["Ananda College", "Nalanda College"]
It is taken from a database. I have selected one column and taken all its entries. Now I need to put this list into a select tag! Select each one from the select box. How can I do this?
I tried to add a each do block inside the select_tag and it didn't work.
PS : The list is dynamically generated
You will need to use the form helper options_for_select which takes an array.
A common way of doing this is setting an array in the controller:
#colleges = College.uniq.pluck(:name)
Then in your view:
<%= f.select :college, options_for_select(#college) %>
Take a look at the docs for options_for_select for more implementation details.
try this it might be helpful
<%= f.collection_select :college_id, Collage.all, :id,:Name %>

Rails: select_tag

i tried this here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
exactly this:
<%= collection_select(:sportlers, :sportlers_id, Sportler.all, :id, :name) %>
But there dont is written, how to get the selected value from the collection_select.
Im helpless, do you work with somethings like this?
I think collection_select will take the first two parameters, the object and the method, and use the values to make a hash, so in your case, you can access the values in your controllers like:
params[:sportlers][:sportlers_id]

Rails: Fields_for without a scope?

Is there a way to use fields_for with in a form without having a scope?
For example:
<% fields_for "user[]" do |x|
<%= x.text_field :name %>
<% end %>
Without the user model being loaded in memory?
I got it working using territory[user][][name], but I would like to keep it in ERB.
I think the answer would be 'no', since those form_for and fields_for would try to determine default value from that given instance variable.
However, I think if you want to lower memory usage from loading that model, you might try to create a mock-up model to return nil values, and create a instance object from that one instead.
is there any specific reason you need to use form_for specifically? Its really designed to be used with an instantiated model object.
Alternatively, why don't you just use the regular form helper tags. You can define it as follows:
<%form_tag :my_form do %>
<%= text_field_tag :foo, :bar %>
<%end%>
You may want to check the documentation for action view to see how it all works.

Resources