Rails form select with NULL (no choice) support - ruby-on-rails-3

How can I add a NULL option to my form select? I have a table:
categories
id
category_id
name
If I'm creating a new category, I want to be able to select the NO_CATEGORY option (NULL value and id).
My view code:
<%= f.collection_select :supercategory_id, Category.all, :id , :name %>
Also, it is a good idea? Isn't it better to have some predefined ROOT category in the database? Thank you.

Try:
<%= f.collection_select :supercategory_id, Category.all, :id , :name, :include_blank => true %>
Its ok to have null. Just have your model logic know that it should create a new category and assign it rather than mass assign from the select. Might be something that happens in a before_validation method

Related

Query model for all possible values of a column - Ruby Rails [duplicate]

How can I get unique values from column in the table?
For example, I have this Products table:
ID NAME CATEGORY
1 name1 1st_cat
2 name2 2nd_cat
3 name3 1st_cat
Here I want to get only 2 values - 1st_cat and 2nd_cat:
<%Products.each do |p|%>
<%=p.category%>
<%end%>
Two more ways:
Product.select(:category).map(&:category).uniq # Ruby does the work
Product.uniq.pluck(:category) # DB does the work (superior)
For Rails >= 5.1 use:
Product.distinct.pluck(:category) # DB does the work (superior)
...because Relation#uniq was deprecated.
I think you can do this:
<% Products.select("DISTINCT(CATEGORY)").each do |p| %>
<%= p.category %>
<% end %>
Source: http://guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields
This does all the work in the database server. The result is a simple array.
<% Product.distinct(:category).pluck(:category).each do |category|
<%= category %>
<% end %>
Rails will generate SQL that works on any database (Postgres, MySQL, etc).
SELECT DISTINCT "products"."category" FROM "products"
I suggest to use Products.all.distinct.pluck(:category) because uniq has been deprecated since rails 5 and it will be removed on rails 5.1
Try this (in the rails console)
Product.group(:category)
Product.group(:category).each { |p| p.name }
For postgres
<% Product.select("DISTINCT ON (category) *").each do |category|
<%= category %>
<%= name %>
<% end %>
Update
even better
<% Product.select(%(DISTINCT ON (category) "#{Product.table_name}".*)).each do |category|
<%= category %>
<%= name %>
<% end %>
because it can return wrong columns when you do joins (e.g. returns id column from joined table, but not products)
If you or anyone want to get two or more attributes from a table like products, based on a distinct feature of an attribute, only this solution will help you for Rails >= 5.1
distinct_products = Product.select("DISTINCT ON (category) *")
# it's an active record relation class.
> distinct_products.class
=> Product::ActiveRecord_Relation
N.B. Don't use .pluck() on the distinct_products. It will reselect from the products table and the distinct feature will not work anymore.
Needed to get unique output and was trying the 'uniq' method unsuccessfully. Tried several solutions posted here unsuccessfully. I'm using devise which gives me access to the current_user method and working with two tables, one being a join (an item has_many :things).
This solution ultimately worked for me :
#current_user.things.select(:item_fk).distinct.each do |thing|
<%= thing.item.attribute %>
<% end %>

Formating Active admin table data with a function

i have a resource defined for active admin like so. All I want to do is format the phone with the rails *number_to_phone* function. However either the documentation is not clear, or my still limited ruby/rails vocabulary is lacking the proper term.
ActiveAdmin.register Voter do
menu :label => "Voters"
index do
column :id
column :last_name
column :first_name
column :middle_name
column :suffix
column :address
column :city
column :state
column :zip
column :zip4
column :phone
end
end
per Rails Cast #284
column :phone, :phone do |voter|
number_to_phone voter.phone
end

Rails 3 HABTM not changing created_at and updated_at fields

I have two models: Product and Categories. They have a HABTM relationship.
At products form i have:
<% Category.find(:all).each do |c| %>
<%= check_box_tag 'product[category_ids][]', c.id, #product.category_ids.include?(c.id) %> <%=c.name%>
<% end %>
It works perfectly but when a categories_products record is created, the created_at field is empty ( 0000-00-00 00:00:00 ).
How can i make Rails create/update those fields?
Thanks.

Groups and subgroups in Rails

I'm currently sorting products based on release date this way:
<% #product_months.each do |month, products| %>
<h2><%= month.strftime("%B %Y") %></h2>
<% products.each do |product| %>
<p><b><%= product.title %></b> on <%= product.street_date.to_date.to_s(:long) %></p>
<% end %>
<% end %>
In the controller:
#products = Product.where('street_date > ?', Date.today).order('street_date ASC')
#product_months = #products.group_by { |t| t.street_date.beginning_of_month }
Products, though, are also grouped into three sales_seaons, spring, summer and fall. I'd like to further group the items into their seaons, like this. Year > Season > Product, instead of Month > Product, but the grouping is proving a little too complicated for me to get. Any thoughts?
Scopes are your friend. They add class level methods to the model they're defined in. The best part about them is that they're chainable.
There are many ways to get the groupings you're talking about but here's what I would start with.
class Product
# ... your code here
scope :by_sales_season, lambda {|season| where('sales_season = ?', season}
scope :by_year, lambda {|year| where('street_date >= ?' DateTime.now(year) }
# ... more of your code
end
Then in your controller you could:
#products = Product.by_year(2012).by_sales_season("spring")
Of course this will not return to all products across every year and/or every season, though the scopes could be modified to do such a thing, or you could loop over the years and seasons accordingly. However, if you do it this way versus using the group_by method which is an Enumerable method then you don't risk pulling the entire result set into memory when you only want a subset of it.

How to group collection by columns with rails

My table named stocks contains product_id, color_id, storage_id and in_stock as columns.
For a given product I want to group all stocks by storage, then for each storage I want to show product (color): in_stock
How should I write a method and how to render?
<% Stock.all.group_by(&:storage).each do |storage, products| %>
Storage: <%= storage %>
<% products.each do |product| %>
(<%= product.color_id %>): <%= product.in_stock %>
<% end %>
<% end %>
Edit: updated to use ERB, and in_stock as a number and not a boolean yes/no column, and use color_id in place of color.name because that assumes you have a relationship to a color.
Not structured exactly how you need it (not enough info from your post), but something like this perhaps?
items = Stock.select('color_id, in_stock').group('stocks.storage_id').where(:product_id => foo)
items.each {|item| puts "(#{lookup_color(item.color_id)}): #{in_stock}"