I have a dropdown with options_from_collection_for_select tag.I want to have the values in the dropdown list sorted by ascending order. (it's a list of cusromer names,just want them sorted by customer name in ascending order). This is what I have:
.row
.col-md-3
= label_tag "Customer/Supplier Name"
= select_tag "search_customer_supplier[id]", options_from_collection_for_select(Organization.customers_and_suppliers, :id, :name, params.dig('search_customer_supplier', 'id')), class: "form-control parent_class chosen-select", id: "search_registered_customers", include_blank: true
In my model the customers_and_suppliers method difined as a scope. Here it is
scope :customers_and_suppliers, -> { joins(accounts_dealer_types: :dealer_type).where(mst_dealer_types: {code: Organization::TYPES}) }
Finally i tried using this code
scope :customers_and_suppliers, -> { joins(accounts_dealer_types: :dealer_type).where(mst_dealer_types: {code: Organization::TYPES}).order('name ASC') }
Then i got the results as ascending order.But in the dropdown there are so many duplicated records displaying,What will be the solution ?
Related
So we have a users table. And we have a account_managers table which belongs to a user. On the users table, there is a role column which is an array that has one or multiple roles a User might have. This select is on the AM edit page. How would I have it to pre-select the roles in the array for the associated user and allow multiple selected values? I tried this but it doesn't work
f.input :role, as: :select,
multiple: true,
collection: ["admin", "reporting", "user", "team_lead"],
selected: f.object.user.try(:role)
:input_html => { :value => f.object.user.try(:role) }
I have scaffold student name:string is_active:boolean and scaffold attendance student_id:integer event_id:integer
Student has_many :attendances
Attendance belongs_to :student
Attendances/_form.html.haml:
= simple_form_for(#attendance) do |f|
= f.error_notification
.form-inputs
= f.association :student
= f.association :event
.form-actions
= f.button :submit
How to edit the association dropdown to see there only students that have is_active : true?
You will want to separate your logic from your views. So, in the controller method that uses the form above you can define what you want to see in your drop down menu:
def controller_method
#active_students = Student.where(is_active: true)
end
and in your association in your form you can specify the drop down menu collection to be equal to #active_students:
f.association :student, collection: #active_students
Alternatively, in one line:
f.association :student, collection: Student.where(is_active: true)
I have the following embedded structure
class CbAuthor
include Mongoid::Document
embeds_many: cb_bylines
field :name, type: String
end
class CbByline
include Mongoid::Document
embedded_in :cb_author
has_many :cb_articles
field :byline, type: String
end
class CbArticle
include Mongoid::Document
belongs_to :cb_byline
end
This is because there are many bylines or pseudonyms the authors publish under and that is will be attached to their analytics reports. So when I have a byline, how do I find the author? This will be necessary because They will have dashboards that should list all the articles they wrote under all their respective bylines.
I tried CbAuthor.cb_bylines but that gives me a no method error. or CbAuthor.where(cb_bylines["byline"]: bylineInQuestion) but that also gives errors.
Essentially the goal is to have one author name to find all his bylines and the articles associated with those bylines
embeds_many :cb_bylines is just a fancy way of saying "add an array of hashes called cb_bylines" (at least as far as storage is concerned). That means that your CbAuthors look like this inside MongoDB:
{
_id: '...',
name: '...',
cb_bylines: [
{ _id: '...', byline: '...' },
...
]
}
MongoDB will unroll the array for simple queries for you so you can simply look for 'cb_bylines.byline' as though you were querying a hash inside the collection:
authors_by_lined_as_pancakes = CbAuthor.where('cb_bylines.byline' => 'Pancakes McGee')
or if you know there is just one:
pancakes_mcgee = CbAuthor.find_by('cb_bylines.byline' => 'Pancakes McGee')
Don't be afraid to bypass Rails and Mongoid to look at what your data really looks like inside MongoDB.
Recently I came across issue I cannot resolve (or google it properly). First, here are the files:
#Counter.rb
class Counter
include Mongoid::Document
embeds_many :pointing, as: :goodvs, store_as: "goodvs"
embeds_many :pointing, as: :badvs, store_as: "badvs"
accepts_nested_attributes_for :pointing
field :name, type: String
field :champId, type: Integer
end
#Pointing.rb
class Pointing
include Mongoid::Document
belongs_to :counter
accepts_nested_attributes_for :counter
field :name, type: String
field :votes, type: Integer
field :lane, type: String
end
Description
I want to nest Pointing class in Counter class double to make structure like this:
{
name: 'sth',
champId: 1,
goodvs: [{
name: 'sthsth'
votes: 1
lane: 'top'
},
{
name: 'sthsth2'
votes: 4
lane: 'bot'
}],
badvs: [{
name: 'sthsth'
votes: 1
lane: 'mid'
}]
}
Anyone have any solution how to do this? I can make normal structure for nested attributes used once only but I have no clue how to do this properly for this situation.
I have only just started messing around with mongo/mongoid myself but it looks like your class definition is a bit awry. The details are referenced from the Mongoid Relations docs
Use the embedded_in relationship for embeds_many. belongs_to goes with has_many.
class Pointing
include Mongoid::Document
embedded_in :counter
field :name, type: String
field :votes, type: Integer
field :lane, type: String
end
If that doesn't fix it... I setup custom relation names slightly differently by using class_name to point back to the actual class. The as: option is documented to be used when the the child document can belong to many parents but I've not used it enough to say if this is an actual difference or just style.
class Counter
include Mongoid::Document
embeds_many :goodvs, class_name: "Pointing"
embeds_many :badvs, class_name: "Pointing"
accepts_nested_attributes_for :goodvs, :badvs
field :name, type: String
field :champId, type: Integer
end
Then retrieving the objects I've created with:
Counter.each do |c|
log 'counter name', c.name, c.id
log 'goodv', c.goodvs
log 'goodv first', c.goodvs.first.name, c.goodvs.first.id
log 'badvs', c.badvs
log 'badvs first', c.badvs.first.name, c.badvs.first.id
end
Results in:
counter name [sth] [53cfcee66fcb2d2db5000001]
goodv [#<Pointing:0x00000601a395b0>] [#<Pointing:0x00000601a393f8>]
goodv first [mee] [53cfcee66fcb2d2db5000002]
badvs [#<Pointing:0x00000601a37468>] [#<Pointing:0x00000601a372b0>]
badvs first [mee] [53cfcee66fcb2d2db5000002]
So different Pointing object references but both goodvs and badvs contain the same mongo document underneath.
I'm still getting my head around MongoDB and Mongoid in particlar.
Let's say I have a User and each User has one Thingamajig. When I create the User
I want the system to autmatically also create a blank Thingamajig for that User.
Each Thingamajig has a whatsit field that must be unique if it has a value, but is allowed to have no value when created.
So I define the following classes.
class Thingamajig
include Mongoid::Document
field :whatsit, type: String
index({whatsit: 1}, {unique: true, name: 'whatsit_index'})
end
class User
include Mongoid::Document
field :name, type: String
index({name: 1}, {unique: true, name: 'user_name_index'})
embeds_one :thingamajig, dependent: :nullify, autobuild: true
end
However what I find when I
User.create!(name: 'some name')
is that User.find(name: 'some name').thingamajig is nil.
Questions:
How can I ensure that each User gets an associated Thingamajig? and
How do I specify that the name field of a User is required?
FYI I am using Sintara not Rails (if that matters to anyone).
1 - The autobuild: true option normally should have done the trick. I think the problem is that you forgot to add the other side of the relation to the Thingamajig model:
class Thingamajig
include Mongoid::Document
embedded_in :user
...
end
2 - To specify required fields, use validations:
class User
include Mongoid::Document
field :name, type: String
validates_presence_of :name
...
end
Mongoid uses ActiveModel validations.