How to make a default:Rails - ruby-on-rails

I have a 10 products but I want particular product default on display. Example If type http://localhost/products then product display should be product xyz.
Thanks,

I'd create a new boolean column like :default_product, then in the model use a scope like "scope :feature_product, first(:conditions => {:default_product => true})". Then, in the controller, use :feature_product to instead of all by doing Product.feature_product in the index method.

It looks like you are using REST so in your products controller:
def index
#products = []
#products << p1 = Product.find_by_name('name')
#products << p2 = Product.find_by_name('name')
#products << p3 = Product.find_by_name('name')
end

Related

How to get all records of all parent records

I'm needing to create a select box for a form where I list all child records of all parents. How do I do this in my controller? I'm thinking the answer below, but its returning "undefined method nil:NilClass"
before_action :get_children
def get_children
#parents = current_user.parents.all
#children = #parents.children.all
end
#parents is an array so the children of that array would be nil.
So you would have to use some type of loop like:
def get_children
children = []
#parents = current.user.parents.all
#parents.each do |child|
children << child
end
return children
end
Something like this oughta work.
If you want an array:
#children = #parents.map(&:children).flatten.uniq
If you want an ActiveRecord::Relation (assuming your model is called Thing, and each thing belongs_to parent):
#children = Thing.where(parent_id: #parents)
You can do the following:
def get_children
#parents = current_user.parents.all
#children = Children.where(parent_id: #parents.pluck(:id).uniq)
end

How can I iterate through a model then iterate again in my view?

I want to pull data for each of my users. I grab their person_id from my user table, then use each person's ID to figure out how many days each person has available, and show that in my view.
I'm not sure if I am doing this correctly because I am iterating in my controller then again in my view.
def how_many_days_users_have
#my_group = User.all.pluck(:person_id)
#my_group.each do |v|
#indirect_id_v = Empaccrl.where("person_id = ? and is_active = ?", '#{v]', 'Y').pluck(:a_code).first
#v_range = Empaccrl.where("person_id = ? and is_active = ?", '#{v]', 'Y').pluck(:ac).first
#v_range_taken = Empaccrl.where("person_id = ? and is_active = ?", '#{v]', 'Y').pluck(:taken).first
#total_v_hours = #v_range.to_d - #v_range_taken.to_d
#total_v_days = #total_v_hours / 8
end
Then in my view I use this to show me this data:
%tr.trace-table
-#indirect_id_v.each do |idd|
%tr.trace-table
%td.trace-table{:style => 'border: solid black;'}= idd
-#total_v_days.each do |days|
%tr.trace-table
%td.trace-table{:style => 'border: solid black;'}= days
Okay, first things first, move some of that junk to your model, like so:
class Empaccrl < ActiveRecord::Base
def self.all_people
where(person_id: User.all.pluck(:person_id))
end
def self.active_people
all_people.where(is_active: 'Y')
end
def self.active_vacation_data
active_people.select(:person_id, :ac, :taken)
end
def total_v_hours
ac.to_d - taken.to_d
end
def total_v_days
total_v_hours / 8
end
end
Then you can use:
peoples_vacation_information = Empaccrl.active_vacation_data.all
peoples_vacation_information.map do |person|
p "person #{person.person_id} has #{person.total_v_days} vacation days"
end
Honestly, you don't even need all that, but I'm not sure why you are doing what you are doing, so I figured better be safe and add stuff. Whatever you don't need, just ignore.

Call the same function on a list and return a list with no duplicates?

I have this function:
medIntCategory = MedicalInterventionCategory.find_by_category_text(category.category.text)
However now I have a list of categories called categories.
I would like to execute the above code for each category and get back a list of medIntCategories, but with no duplicates.
Is there a simple way to do this since I am only dealing with integers?
in simple terms:
categoryList = []
for each category in categories do
categoryList += MedicalInterventionCategory.find_by_category_text(category.category.text)
end
But with duplicate checking
This sounds like a job for Array#map and Array#uniq:
category_list = categories.map{|category|
MedicalInterventionCategory.find_by_category_text(category.category.text)
}.uniq
#result=Array.new
##assuming that it returns an array
medIntCategory = MedicalInterventionCategory.find_by_category_text(category.category.text)
##get the first category obtained
#result << medIntCategory
if medIntCategory.present?
medIntCategory.each do |m|
##add in same array only if not present
if !#result.include?(m)
#result << m.find_by_category_text(c.category.text)
end
end
##return a unique value array
#result.flatten.compact.uniq unless #result.blank?
end
HOPE IT HELPS
I think this would work
category_list = []
categories.each do |category|
category_list << MedicalInterventionCategory.find_by_category_text(category.category.text).distinct
end

Query All Distinct , wrong number of arguments

in my application each activity has a sport, and a user has many activities, i'm trying to list all the distinct sports that a user practices through activities.The name of the activity is also the name of the sport which the activity is associated with.
Its giving me the following error:
wrong number of arguments (1 for 0)
def show_other
#account = Account.find(params[:id])
#activity = #account.activities.last(20)
#sports = #activity.select(:name).distinct //error here//
end
How i call this view:
<%= link_to friend.name, pages_show_other_path(:id => friend.id) %>
Any help would be appreciated :)
last() will always return an array and you can't use that select on an array.
Try something like this:
Activity.where(account_id: params[:id]).pluck(:name).last(20).uniq
Is this what you're looking for?:
def show_other
#account = Account.joins(:activities).find(params[:id])
#activities = #account.activities.uniq.last(20)
#sports = #activities.map(&:name)
end

Add extra filter parameters in RoR

Suppose I have a method in a controller:
def my_find(is_published, count)
items = Idea.where(published: is_published)
#......
end
Sometimes I want to pass some extra filter arguments
def my_find(is_published, count, some_extra_filter = nil)
items = Idea.where(published: is_published) #.where (some_extra_filter)
#......
end
where some_extra_filter can be lambda or just an plain sql "where" string and it can also be nil or "".
So how do I concatenate .where(published: is_published) with where (some_extra_filter) to get what I need?
This is actually very easy using scopes:
def my_find
#items = Idea.scoped
#items = #items.where(published: is_published) unless is_published.nil?
#items = #items.where(other: other_param) if other_params < 10
# etc, etc
end

Resources