I'm trying to customize an enum field with something else than only the name.
For example my entity record from database have columns as: name, postal_code, id etc ..
and I would like to have something like this in the dropdown "#{name} #{postal_code}, #{department}
I'm doing this:
field :city, :enum do
enum do
# Here I would like to get the entity from DB in order to have all
# columns to something similar as =>
entity.collect.each { |c| "#{c.name} (#{c.postal_code}), #
{c.department.name}"}
end
end
but I don't know how to get the active records (entity in my example) of the actual value of City entity.
How can I do this?
note: that department belongs to another model who is associated to City
Considering the OP comments i'm going to asume that the model in question has this association defined like this:
belongs_to :city
And the city field itself something like this
rails_admin do
edit do
field :city
end
end
Because this way rails admin will render a select drop down that will allow you to search the cities without loading them all.
After that on the City model you can define the title method, quoting the docs
By default it tries to call "name" or "title" methods on the record in question. If the object responds to neither, then the label will be constructed from the model's classname appended with its database identifier. You can add label methods (or replace the default [:name, :title]) with:
RailsAdmin.config {|c| c.label_methods << :rails_admin_title }
The title method can then be defined
class City < ApplicationRecord
def rails_admin_title
"#{self.name} (#{self.postal_code}), #{self.department.name}"
end
end
In a related issue, you will probably want to configure how rails admin searches for the city, i'll just link to the docs.
You can do it, but you need to define a name and a (stable)value for each select drop down, a simple hash out to do it like this:
field :city, :enum do
enum do
entity = bindings[:object]
City.all.map do |c|
{c.id => "#{c.name} (#{c.postal_code}), #{c.department.name}"}
end
end
end
Im assuming each city has an id, but you can use any value you want to store on that field on your DB.
So your users would see the nice formatted string, but rails admin would post the form with the city id on the city field.
Related
After trying for hours, I am coming here. I have the following model:
class Items(models.Model):
date=models.DateField()
giver=models.IntegerField()
Class Givers(models.Model):
name=models.CharField(max_length=25)
Basically, an item could be given by donators from Givers model or it could be bought by user. If given, giver field stores id from Givers model. Else, it is 0. Now I want a query that will list out my items. If giver>0, I need to show the name of the giver. I can't create relationships here. This query has brought me so close and works like a charm on literal values but I want to provide the 'giver' field instead:
data=Items.objects.all().extra(select={'giver_name':giver_name(1,False)}).values('giver_name','id','date')
It works like a charm even if given any number (exists or not exists). But I want to do something like this but it fails cos field is not found or undefined:
data=Items.objects.all().extra(select={'giver_name':giver_name(giver,False)}).values('giver_name','id','date')
giver_name is a function that accepts id of a giver and a boolean value and returns a name.
I have a table called key_words which has one column called name.
Then I have another table called publications with a column called key_words which is of array type.
When I create a publication the value saved in key_words is something like this ["1", "29", "40"] (these are ids saved in key_words table)
How can I find a publication if I type for example computers in a searchable input?
In this moment I have a search_form_for to find by the publication title and description.
= search_form_for #search, url: publications_path do |f|
#custom-search-input
.input-group.col-md-12
= f.input :title_or_short_description_or_description_cont, input_html: { class: 'form-control input-sm' }, placeholder: 'Buscar', label: false
I would create a third table key_words_publications to store the relationship instead of the array column. Check out the docs for HABTM here.
class Publication
has_and_belongs_to_many :key_words
end
class KeyWord
has_and_belongs_to_many :publications
end
Then you should be able to do something like:
key_words_name_cont
Part 1: What i want is to fetch records of two tables in one collection select. Later, i want to perform search based on selected item.
So far i have managed to get the records in this manner in one select:
Controller:
#result1 = Model1.all
#result2 = Model2.all
#all = #result2 | #result1
View:
<%= collection_select :id,:id,#all, :id, :id,{prompt: "All Templates"} %>
The problem here is i want to display the name form Model1 and type from Model2.
Part 2 If the user selects the name, i want to get record from Model1 and if the type is selected, i want to get records form Model2.
All i am able to get is the id of both the models in one collection select. I am out of ideas. Let me know if any more details are required. Any help is appreciated. Thanks.
You've supplied :id to collection_select for the text_method. Check the docs to see how this helper works.
One solution would be to create an 'alias' method in each of your models which you can then call in collection_select:
model1.rb
class Model1
def text_value
name
end
end
model2.rb
class Model2
def text_value
type
end
end
I've named the method, text_value, for demonstration purposes. You may need to come up with a different name for that attribute.
Incidentally type as an attribute is reserved for Single Table Inheritance tables so it would be better to use a different attribute name.
in the view
<%= collection_select :id,:id, #all, :id, :text_value, {prompt: "All Templates"} %>
I have the following situation. I have two table Student and Course linked via HABTM association.
On Student form, I want to have a list of Courses, however I need more information displayed than a simple checkbox and a label next to it. Essentially, I want to have an HTML table with information from my Course model with a checkbox for the association in the first column of the table.
How can this be accomplished?
Essentially this did it:
I iterated through my #courses collection and created each checkbox manually like this:
check_box_tag "student[course_ids][]", course.id
In my controller params I added this:
params.require[:student].permit(... ,course_ids: [])
My Student model needed this:
accepts_nested_attributes_for :courses
Pretty simple if you know what you're doing :-)
I am wondering, what would be the best option to implement a virtual model.
Let's say we have users, we would like them to have a gender attribute.
The easiest option would be to have a Gender model and then do a belongs_to :gender
Then, each time you ask for a user, it will also perform a SELECT on the genders table
Therefore, does anyone have an idea on how to implement a virtual model that would not be persisted in the database and just be a regular model with the ability through the belongs_to association to perform a select on Male or female ?
I know I could just use Serialization but I feel like it's a bit of a waste of memory/space/ressources to store a serialized object each time I create a user and would definitely love a solution where I can use the traditional gender_id:integer ...
Thank you for your help guys.
I suggest you use enum field (Rails 4.1+), which allows its value to be within a predefined set of values (you can enumerate them, hence the name). It is persisted as a plain integer in database, but convenience methods are generated for easily manipulating them:
class User < ActiveRecord::Base
enum gender: [ :male, :female ]
end
user.male!
user.female? # => false
user.gender # => "male"
User.male # => a collection of users with "male" specified in gender
Conversation.genders # => { "male" => 0, "female" => 1 }