Using ! with object name in ruby on rails - ruby-on-rails

I have a column "SchoolName" in my educations table and I want to check the following code:
<% #user_education.each do |e| %>
<% if !e.SchoolName.nil? and e.Private? == "1" %>
<%= check_box(:ChkBx_Education, "", true) %>
<%= label(:lb_Profession, "Education") %>
<% else %>
<%= check_box(:ChkBx_Education,"" ) %>
<%= label(:lb_Profession, "Education") %>
<% end %>
<% end %>
The #user_education variable comes from the controller page i.e #user_education=Education.where(:UserID => current_user.id) My question is whether this statement !e.SchoolName.nil? is right or wrong? I want to ensure that if SchoolName is not nil and Private? is equal to "1" then it checks the checkbox.

As per the comments, you need to rename your database columns to be lower case in order to make initial syntax improvements:
<% #user_education.each do |e| %>
<% if e.school_name && e.private? %> =-> .private? should return true / false if column is boolean
<%= check_box(:checkbox_education, "", true) %>
<%= label(:checkbox_education, "Education") %>
<% else %>
<%= check_box(:checkbox_education,"") %>
<%= label(:checkbox_education, "Education") %>
<% end %>
<% end %>

Buddy, you have broken the rules of developing an application with Ruby.
Active Record uses naming conventions for the columns in database tables, depending on the purpose of these columns.
The names of attributes in Ruby should always be uncapitalised, just like local variables.
Foreign keys - These should be named following this pattern: singularized_table_name_id (ex: item_id, order_id). These are the fields that Active Record will look for when you create associations between your models.
Primary keys - By default, Active Record will use an integer column named id as the table's primary key. When using Rails Migrations to create your tables, this column will be automatically created.
I think you should refer to Ruby Guides and read it carefully before working with any application.

The correct convention of the following line would be something like this, taking Private? as a boolean columns. The correct code following rails convention would have been something like this
<% if e.school_name.present? and e.is_private? %>

Related

Scope of each on rails template

I'm new to rails and I'm trying to build a view that will list the parents and related children
Ex:
Passport has many Visas
I want to list information about the passport and the visas that the passport has.
So I have
<% #passport_list.each do |passport| %>
# passportFields
<% passport.visas.each do |visa| %>
<%= t.text_field :visa_type %>
<% end %>
<% end %>
I'm getting the error
undefined method `visa_type' for #Passport:0x000000091b8b28
It looks like rails is trying to find the property visa_type for passport, instead of in visa. How does the scope work within each? Can I force it to access visa_type from visa?
I think you're looking for the fields_for form helper. This will allow you to create fields for the relevant visa attributes. Replace your code sample with the following, and you should be all set.
<% #passport_list.each do |passport| %>
# passportFields
<% t.fields_for :visas do |visa_fields| %>
<%= visa_fields.text_field :visa_type %>
<% end %>
<% end %>
You can also iterate over the list as follows:
<% #passport_list.each do |passport| %>
# passportFields
<% passport.visas.each do |visa| %>
<% t.fields_for :visas do |visa_fields| %>
<%= visa_fields.text_field :visa_type %>
<% end %>
<% end %>
<% end %>
For more information on fields_for, check out the link I added above, and to customize further for your use case, check out the "One-to-many" section.
IMO you should always handle the null case of an object.
Something like this if you use rails (present? is a Rails function)...
<% if #passport_list.present? %>
<% #passport_list.each do |passport| %>
passportFields
<% passport.visas.each do |visa| %>
<%= t.text_field :visa_type %>
<%end%>
<%end%>
<% else %>
<p>Nothing to see here</p>
<% end %>
However if your #passport_list is backed by an ActiveRecord Query, you can handle this in the model/helper/controller by returning the .none query on the model. Note that this differs from an empty array because it is an ActiveRecord Scope, so you can chain AR queries onto it
# scope on AR model
def self.awesomeville
where(country_of_origin: "awesomeville")
end
# method queried in controller
#passport_list = Passport.all
if #passport_list.present?
#passport_list
else
Passport.none
end
# additional filtering in view is now possible without fear of NoMethodError
#passport_list.awesomeville
Whereas a ruby Array would raise an error as it would respond to the Array methods.

Ruby Rails change all the fields using form

How do I change all count value at same time, currently i have to change each of them individually.
<% #book.each do |book| %>
<%= form_for(book) do |f| %>
<%= f.number_field :count %>
<% end %>
<% end %>
lets say my current value 2,5,1 and when i change :count field to 3, result should be like 3,3,3 to all. Thanks
You need to be more specific.
if you need to update a column for all rows use this
Book.update_all(:count, 3)
or on book controller
Book.update_all(:count, params[:book][:count])

Ruby & Rails - conditional count within block

This is a simple question but I can't figure out as I am new to rails.
My controller has a #neighborhoods variable which contains neighborhood records for each Business in the #businesses variable (each business has_one neighborhood)
In my view, I want to print out:
Each unique neighborhood name
How many of each unique neighborhood name (can be multiple since it is taken from the #businesses variable)
Currently I have:
<% #neighborhoods.uniq{|x| x.name}.each do |neighborhood| %>
<p><%= neighborhood.name %></p>
<%= #neighborhoods.where{name = neighborhood}.count %>
<% end %>
I know the above code is wrong, but it illustrates what I am trying to do. How can I achieve this?
<% #neighborhoods.group_by(&:name).each do |name, neighbourhoods| %>
<p><%= name %></p>
<%= neighbourhoods.count %>
<% end %>

Rails finding a value in a table

Is it possible to call the include? function on a whole table, like this?
<% #user.games.each do |g|
##latestround = g.rounds.order('created_at DESC').first
%>
<% if ##latestround.submittedpictures.isFinalPicture.include?(true) %>
<p>FinalPicture has been played!</p>
<% end %>
<% end %>
The problem i'm getting is that It only works when I put a block on submittedpictures and then loop through each record of this table. However I want to look through the whole table in one go and see if the column 'isFinalPicture' includes a value with 'false'.
Any ideas?
The following snippet works but its not the way i want it (I would get more lines if the round happens to have more 'true' FinalPictures)
<% ##latestround.submittedpictures.each do |s| %>
<% if s.isFinalPicture == true %>
<p>Final Picture has been played!</p>
<% end %>
<% end %>
You could make a scope for it like
class SubmitedPricture << ActiveRecord::Base
scope :final_pictures, where('isFinalPricture = ?', true)
end
then you could see if there is any with only one query
latestround.submittedpictures.final_pictures.any?
Also you should follow the conventions of Rails in naming your Models and everything else. Like submittedpictures should be submitted_pictures

How to create a rails checkbox form?

Im trying to create a checkbox table of about 20 "interests" that lets the user select as many as they want. I have a Interest & User model with a HABTM relationship (through a "interests_users"join table).
So:
How do i seed the interests table (just has a name:string attribute) with the names of 20 or so pre set interests?
How do i display these in a ERB form allowing the user to select as many as they like?
Note.. Im using the Wicked gem to create a multistep form (<-working great)
If you're on Rails >= 3.0, then have a look the db/seeds.rb file. You get to put arbitrary Ruby code in that file, which you run through the Rake task rake db:seed. You can just put a lot of lines like Interest.create :name => 'World Domination'.
This one is going to depend on how you set up your form. Going off the information you've given, I'd do something like this:
<%= form_for #user do |f| -%>
<% Interest.all.each do |i| -%>
<div><%= i.name -%> <%= check_box_tag 'user[interests][]', i.id, #user.interests.detect{|ui| ui.name == i.name} -%></div>
<% end -%>
<% end -%>
In your controller you would then be able to just update your user model's attributes. Be sure to make sure you are able to mass-assign your parameters, and also keep in mind a limitation of the HTML spec with regard to unchecked checkboxes (read the part titled, "Gotcha").
EDIT: fixed some grammar-related typos.
<% for interest in Interest.find(:all) %>
<%= check_box_tag "user[interest_ids][]", interest.id, #user.interests.include?(interest) %>
<%= interest.name %>
<% end %>

Resources