I am using activeadmin gem in my application using Rails 4. I need excel download for which I am making use of activeadmin-axlsx gem.
I am able to get all the columns that exists in my database for the particular model in the excel sheet as its columns. However, what I want is, to add a column which does not exists in my database.
This is what I have tried until now
column('start_date') do |date|
date.start_date
end
Here start_date is an attribute in my db and hence I get that column in the excel.
Now when I try to add another column End Date(which is not an attribute in the db), I get a blank column.
I have also tried the below snippet, referring to the Github link for activeadmin-axlsx
config.xlsx_builder.column('end_date') do |emp|
emp.end_time.strftime("%Y-%m-%d")
end
This gives a blank column in the excel
Can anybody help me achieve this?Or suggest any other gems that can be used with activeadmin gem?
Many Thanks!
The solution is to add the attributes that do not exist in your database as an attribute accessor in your respective model.
For the above example,
end_date is not an attribute in the table, so in the model say Employee, add this
class Employee < ActiveRecord::Base
attr_acessor :end_date
end
Here I am modifying end_time which is an attribute in database of type datetime to fetch only the date with the name of the column being End Date
In the app/admin/employee.rb
ActiveAdmin.register User do
xlsx do
column('end_date') do |emp|
emp.end_time.strftime("%Y-%m-%d")
end
end
end
This would give you the desired value.
I think you can just define one like start_date. Did you try:
column(:end_date) do |resource|
resource.end_time.strftime('%Y-%m-%d')
end
Related
I am using RoR 4.1 with RailsAdmin 0.6.8. I can create custom fields in RA either by defining a method on the model and including it via
field :custom_method
or formatted_value
field :custom_field do
formatted_value{ bindings[:object].something_here }
end
The problem is I cannot sort by that column in list view. Anybody have experience with this?
field :custom_field do
sortable :name #or:lastname or :firstname
formatted_value{ bindings[:object].something_here}
end
There is a good question here explaining how to correctly use ActiveAdmin with associations.
In my situation though I have a customer model has_many associated to a sales model and the sales model is pretty big. So when I try to view my customer page in ActiveAdmin the server is running a call for all sales so that (I am guessing) it can return those associated columns.
This is timing out my server (504 Gateway Time-out ngx_openresty/1.4.3.6).
Is there any way to say to ActiveAdmin to ignore an association for that view? Ie the index view. Once I get to the 'show' view and have isolated a customer it is ok to run the query on that customers sales but running all customers with all sales is not required on the index page.
Hope I have been clear.
Ok I have just realised that without specifying which columns I want in the index on the customer.rb file it will try and grab all including the associated columns (correct me if I am wrong on that).
Either way, before I only had he config.per_page line. By adding index do and my columns it is working properly. That was easy!
ActiveAdmin.register Customer do
config.per_page = 25
index do
selectable_column
id_column
column :customer_code
column :customer_name
column :customer_rep_name
column :created_at
actions
end
filter :customer_rep_name
filter :market_segment_name
end
am Using Apex4.1,
in my application I have one Tabular form which has the following fields,
Emp_id
Emp_name
Dept_id
Here Emp_id is the Updatable column and it is a select list LOV and
Emp_name is a upadatable column,
Here what I need is,
If I select the Emp_id from the LOV ,the Emp_Name should be stored automatically based
on the value selected in EMP_ID,
In tabular form I could not create Dynamic action like creating in normal forms,
Can anyone help me in this issue?
Thank you.
APEX does not currently provide dynamic actions on tabular form items. Hopefully this may be addressed in APEX 4.2 but the Statement of Direction does not explicitly say so.
So for now if you need to do this you will have to write your own Javascript, using the unique IDs of the tabular form items to manipulate them (the IDs look like "fcc_rrrr" where "cc" is the column number and "rrrr" is the row number). See this SO q&q for sample Javascript code that uses these.
The Javascript you need to write is a little daunting (for a beginner), but one thing to note is that in your case you can avoid any need for using AJAX to get the employee name by embedding the name in the return value of the LOV something like this:
select emp_name d, emp_id||':'||emp_name r
from employee
order by 1
This way the return values will look like '123:John Smith'; your Javascript can parse this string and extract 'John Smith' and insert it into the emp_name item on the same row. Obviously you will also need to parse this string to obtain the emp_id value you will need when updating the database when the page is submitted.
Is there a way to filter and get only specific columns?
For example, get all entries with the column first_name.
QuerySet.values() or QuerySet.values_list(), e.g.:
Entry.objects.values('first_name')
If you want a list of only the values, use:
Entry.objects.values_list('first_name', flat=True)
To only get a column's values from the table but still return an object of that model, use only:
record = Entry.objects.only('first_name')
This will defer all other columns from the model but you can still access them all normally.
record.first_name # already retrieved
record.last_name # retrieved on call
I have a model that contains a FileField. I want to search for a specific filename. How do I do it? I was trying:
MyModel.objects.get(document__name=FOO)
I got a Join on field 'document' is not permitted.
Thanks!
The attributes of a FileField are not stored in the database, and cannot be used in a query. For example, the name is simply the upload_to string plus the filename. If you want to store extra data about the file you have to put that data into other fields on the database, as the example documentation shows with a Car having a name of "57 Chevy".
Also, typically the double underscore in Django's ORM denotes following a database relationship, either a ForeignKey or a ManyToMany. So in the example ORM call you provided, I would assume that MyModel had a field document that was either a ForeignKey or ManyToMany to another model, and that other model has a field called name. Which doesn't sound like is the case.
Hope that helps some.
Do this instead:
MyModel.objects.get(document__icontains='FOO')
You can filter on document, and it'll filter by the string that is the path on disk to the file.