Default sorting using jqGrid and Ruby on Rails - ruby-on-rails

Hello
I am using Ruby on rails and JQgrid. Everything works fine but when the grid load the data it is sorted in a way I don't want. I need to be sorted descendent in the ID column.
I was not the one who developed this software so I don't know exactly what to change. This code is in the helper of the quote controller and I think here is where I have to change it. Someone told me to use Quote.order('id DESC') which is in the last line. I put it in the second line but nor of them is working properly. Could someone please tell me what needs to be added in order to accomplish it? There is also code in javascript. But that is very complex. I think I don't need to change that.
I really appreciate. I have more than 2 moths trying to solve it. Thanks.
def format_quote_grid!
Quote.grid.update({
:title => "Quotes",
:pager => true,
:search_toolbar => :hidden,
:resizable => false,
:height => :auto,
:except => [:contacts],
:rows_per_page => 10}) { |grid|
grid.column :id, :label => "Number", :width => 50#, :proc => lambda {|record| link_to record.number, quote_path(record) }
grid.column :job_name, :width => 140, :proc => lambda {|record| link_to record.job_name, quote_path(record) }
grid.column :category, :width => 60
grid.column :needs_installation, :width => 60
grid.column :contact_id, :hidden => true
grid.column :business_id, :hidden => true
grid.column :contact_name, :label => "Contact", :width => 100, :sortable => false, :proc => lambda {|record| link_to(record.contact.name, record.contact) if record.contact.present? }
grid.column :business_name, :label => "Business", :width => 100, :sortable => false, :proc => lambda {|record| link_to(record.business.name, record.business) if record.business.present? }
grid.column :scope_of_work_id, :hidden => true
grid.column :markup, :hidden => true
grid.column :notes, :hidden => true
grid.column :shred, :hidden => true
grid.column :printed_at, :hidden => true
grid.column :created_at, :hidden => true
grid.column :updated_at, :hidden => true
grid.column :user_id, :hidden => true
grid.column :actions, :width => 200, :sortable => false, :searchable => false, :proc => lambda {|record|
permissioned_actions(record) do |p|
p.show_link
p.link('Printable', printable_quote_path(record), :show, record)
p.edit_link
p.destroy_link
p.link('RFQ', request_for_quote_path(record.scope_of_work.request_for_quote_id), :show, record.scope_of_work) if record.scope_of_work.present?
p.correspondence_link
p.resources_link
p.link(record.work_order.number, work_order_path(record)) if record.work_order
end
}
Quote.order('id DESC') #hecho por mi
}
end

Ok, from one of your earlier posts on this subject, it seems you are using Gridify.
Gridify seems to support the following options:
:sort_by # name of sort column of next request
:sort_order # sort direction of next request, 'asc' or 'desc' ('asc')
So, I would add the following just below :except => [:contacts],
:sort_by => :id,
:sort_order => :desc,
Hope this helps.

Related

How to add custom name input field in rails simple_form_for

#tech_data = TagNode.first outputs below object from the controller .
p #tech_data
#<TagNode _id: 5119dcf74054448e576f3392, parent_id: nil, parent_ids: [], name: "categories", path: "categories", _type: "TagNode">
I have a form which has the fields name, _type, and category. But my object doesn't have the field category.
Here's my form
= simple_form_for #tech_data, as: :techtags, :url => view_techtags_technologies_path, :remote => true,:method => :get,:html => {:id => "upsert_techtags",:data => {:spinner => "#tech-ui"}} do |f|
%label.pull-left Type
= f.input :_type, :collection => tech_types, :label => false, :input_html => {:class =>"chzn-select", :data => {:placeholder => "Select Technology Type"}, :multiple => false}
%label.pull-left Name
= f.input :name, :type=>"text", :required => true, :label => false, :placeholder =>"Type the New Technology", :input_html => {:style => "width:265px;margin-bottom:0;"}
%label.pull-left Category
= f.input :category, :collection => [1,2,3,4], :label => false, :input_html => {:class =>"chzn-select", :disabled=>"true",:data => {:placeholder => "Type or select category"}, :multiple => false}
= f.submit "Create", :class => "btn btn-primary pull-right"
My form has an additional field that my object doesn't have. I get an error when the form is loaded, as I don't have category field in my object.
How can I select the category value in the form without adding this field to the model?
You need something like attr_accessor in Ruby,What is attr_accessor in Ruby?
Please include following in your model,which will make to get values from form to controller.
attr_accessor :category

Rails select tag with required true

I have the following select tag
<%= f.select :id, User.find(:all, :conditions => ["manager = ?", false]).collect {|u| [u.username, u.id]}, {:required => true}, {:class => "multiselect", :multiple => true} %>
I try to add :required => true to it, the view renders but :required => true doesn't work!.
Quite a bit late, but for anyone looking, checking at the reference, should go explicitly as the hash, so ruby interpreter doesn't get confused:
select(object, method, choices = nil, options = {}, html_options = {}, &block)
For this special case:
<%= f.select :id,
User.find(:all, :conditions => ["manager = ?", false]).collect {|u| [u.username, u.id]},
{:prompt => 'Select something'},
{:required => true, :class => "multiselect", :multiple => true} %>
Try this:
<%= f.select :id, User.find(:all, :conditions => ["manager = ?", false]).collect {|u| [u.username, u.id]}, {:multiple => true}, :class => "multiselect", :required => true %>

Fuzzy String Matching with Rails (Tire) and ElasticSearch

I have a Rails application that is now set up with ElasticSearch and the Tire gem to do searching on a model and I was wondering how I should set up my application to do fuzzy string matching on certain indexes in the model. I have my model set up to index on things like title, description, etc. but I want to do fuzzy string matching on some of those and I'm not sure where to do this at. I will include my code below if you would like to comment! Thanks!
In the controller:
def search
#resource = Resource.search(params[:q], :page => (params[:page] || 1),
:per_page =>15, load: true )
end
In the Model:
class Resource < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
belongs_to :user
has_many :resource_views, :class_name => 'UserResourceView'
has_reputation :votes, source: :user, aggregated_by: :sum
attr_accessible :title, :description, :link, :tag_list, :user_id, :youtubeID
acts_as_taggable
mapping do
indexes :id, :index => :not_analyzed
indexes :title, :analyzer => 'snowball', :boost => 40
indexes :tag_list, :analyzer => 'snowball', :boost => 8
indexes :description, :analyzer => 'snowball', :boost => 2
indexes :user_id, :analyzer => 'snowball'
end
end
Try creating custom analyzers to achieve other stemming features, etc.
Check out my example (this example also uses Mongoid & attachments, don't look at it if you don't need it):
class Document
include Mongoid::Document
include Mongoid::Timestamps
include Tire::Model::Search
include Tire::Model::Callbacks
field :filename, type: String
field :md5, type: String
field :tags, type: String
field :size, type: String
index({md5: 1}, {unique: true})
validates_uniqueness_of :md5
DEFAULT_PAGE_SIZE = 10
settings :analysis => {
:filter => {
:ngram_filter => {
:type => "edgeNGram",
:min_gram => 2,
:max_gram => 12
},
:custom_word_delimiter => {
:type => "word_delimiter",
:preserve_original => "true",
:catenate_all => "true",
}
}, :analyzer => {
:index_ngram_analyzer => {
:type => "custom",
:tokenizer => "standard",
:filter => ["lowercase", "ngram_filter", "asciifolding", "custom_word_delimiter"]
},
:search_ngram_analyzer => {
:type => "custom",
:tokenizer => "standard",
:filter => ["standard", "lowercase", "ngram_filter", "custom_word_delimiter"]
},
:suggestions => {
:tokenizer => "standard",
:filter => ["suggestions_shingle"]
}
}
} do
mapping {
indexes :id, index: :not_analyzed
indexes :filename, :type => 'string', :store => 'yes', :boost => 100, :search_analyzer => :search_ngram_analyzer, :index_analyzer => :index_ngram_analyzer
indexes :tags, :type => 'string', :store => 'yes', :search_analyzer => :search_ngram_analyzer, :index_analyzer => :index_ngram_analyzer
indexes :attachment, :type => 'attachment',
:fields => {
:content_type => {:store => 'yes'},
:author => {:store => 'yes', :analyzer => 'keyword'},
:title => {:store => 'yes'},
:attachment => {:term_vector => 'with_positions_offsets', :boost => 90, :store => 'yes', :search_analyzer => :search_ngram_analyzer, :index_analyzer => :index_ngram_analyzer},
:date => {:store => 'yes'}
}
}
end
def to_indexed_json
self.to_json(:methods => [:attachment])
end
def attachment
path_to_file = "#{Rails.application.config.document_library}#{path}/#{filename}"
Base64.encode64(open(path_to_file) { |file| file.read })
end
def self.search(query, options)
tire.search do
query { string "#{query}", :default_operator => :AND, :default_field => 'attachment', :fields => ['filename', 'attachment', 'tags'] }
highlight :attachment
page = (options[:page] || 1).to_i
search_size = options[:per_page] || DEFAULT_PAGE_SIZE
from (page -1) * search_size
size search_size
sort { by :_score, :desc }
if (options[:facet])
filter :terms, :tags => [options[:facet]]
facet 'global-tags', :global => true do
terms :tags
end
facet 'current-tags' do
terms :tags
end
end
end
end
end
Hope it helps,

Ruby. How to sort by a specific column in a grid? [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Default sorting using jqGrid and
def format_quote_grid!
Quote.grid.update({
:title => "Quotes",
:pager => true,
:search_toolbar => :hidden,
:resizable => false,
:height => :auto,
:except => [:contacts],
:rows_per_page => 10}) { |grid|
grid.column :id, :label => "Number", :width => 50#, :proc => lambda {|record| link_to record.number, quote_path(record) }
grid.column :job_name, :width => 140, :proc => lambda {|record| link_to record.job_name, quote_path(record) }
grid.column :category, :width => 60
grid.column :needs_installation, :width => 60
grid.column :contact_id, :hidden => true
grid.column :business_id, :hidden => true
grid.column :contact_name, :label => "Contact", :width => 100, :sortable => false, :proc => lambda {|record| link_to(record.contact.name, record.contact) if record.contact.present? }
grid.column :business_name, :label => "Business", :width => 100, :sortable => false, :proc => lambda {|record| link_to(record.business.name, record.business) if record.business.present? }
grid.column :scope_of_work_id, :hidden => true
grid.column :markup, :hidden => true
grid.column :notes, :hidden => true
grid.column :shred, :hidden => true
grid.column :printed_at, :hidden => true
grid.column :created_at, :hidden => true
grid.column :updated_at, :hidden => true
grid.column :user_id, :hidden => true
grid.column :actions, :width => 200, :sortable => false, :searchable => false, :proc => lambda {|record|
permissioned_actions(record) do |p|
p.show_link
p.link('Printable', printable_quote_path(record), :show, record)
p.edit_link
p.destroy_link
p.link('RFQ', request_for_quote_path(record.scope_of_work.request_for_quote_id), :show, record.scope_of_work) if record.scope_of_work.present?
p.correspondence_link
p.resources_link
p.link(record.work_order.number, work_order_path(record)) if record.work_order
end
}
}
end
Hello, I have this grid in my code. Every time I need to open this grid, the results seems to be random. I need, by default, to be sort by id(number) from the highest to the lowest.
i can not see the way to do it, it seems it has a jquery that makes the sort, but I can not find it, I am new to Ruby and java.
thank you so much for your help.
It seems I am using GRIDIFY as a plugin.
If Quote is an active record model, can you just insert a sorting?
Rails 3:
Quote.order(:id).grid ....

NULL in (NULL) does not match properly

Using Rails 3 active relation, I have a scope:
scope :duplicate_contact, lambda {|contact| where(
:person_id => contact.person_id,
:salutation => contact.salutation,
:first_name => contact.first_name,
:last_name => contact.last_name,
:suffix => contact.suffix,
:birthday => contact.birthday,
:address => contact.address,
:city => contact.city,
:state => contact.state,
:zip => contact.zip,
:phone_1 => [contact.phone_1,contact.phone_2,contact.phone_3],
:phone_1_type => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
:phone_2 => [contact.phone_1,contact.phone_2,contact.phone_3],
:phone_2_type => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
:phone_3 => [contact.phone_1,contact.phone_2,contact.phone_3],
:phone_3_type => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
:email => [contact.email,contact.alternate_email],
:alternate_email => [contact.email,contact.alternate_email]
)
}
This has a problem when :email is NULL. It returns back zero rows, when in fact it should return at least 1 row, ie duplicate_contact(contact).size == 0 is true when it should be false.
I think this has to do with this staement from the mysql docs: "In SQL, the NULL value is never true in comparison to any other value, even NULL."
How can I get this to return the correct result?
One possible solution that I found:
scope :duplicate_contact, lambda {|contact|
q = where(
:person_id => contact.person_id,
:salutation => contact.salutation,
:first_name => contact.first_name,
:last_name => contact.last_name,
:suffix => contact.suffix,
:birthday => contact.birthday,
:address => contact.address,
:city => contact.city,
:state => contact.state,
:zip => contact.zip
)
[contact.phone_1,contact.phone_2,contact.phone_3].compact.each{|p| q=q.has_phone(p)}
[contact.phone_1_type,contact.phone_2_type,contact.phone_3_type].compact.each{|p| q=q.has_phone_type(p)}
[contact.email,contact.alternate_email].compact.each{|p| q=q.has_email(p)}
q
}
scope :has_phone, lambda {|phone|
where("'#{phone}' IN (phone_1,phone_2,phone_3)")
}
scope :has_phone_type, lambda {|phone|
where("'#{phone}' IN (phone_1_type,phone_2_type,phone_3_type)")
}
scope :has_email, lambda {|email|
where("'#{email}' IN (email,alternate_email)")
}

Resources