rails admin belongs to dropdown list - rails-admin

I manage my database from an admin page, and would like to assign a group to indics from a dropdown list.
So what I have for the moment :
indic table:
id | int(11) | PRIMARY
name | varchar(255) |
group_id | int(11) |
group table:
id | int(11) | PRIMARY
name | varchar(255) |
indic.rb:
belongs_to :group
rails_admin do
list do
field :id
field :name
field :group_id
end
edit do
field :name
field :group_id
end
end
group.rb:
has_many :indic
rails_admin do
list do
field :id
field :name
end
edit do
field :name
end
end
With this there is no dropdown (I mean choose group from a list with every group from the database), and I'm not even sure they are linked cause I can put group_id that doesn't exist in group table.
I found on other answer that I need to change the edit field to field :group_id, :belongs_to_association but if I do it I get an error loading the page :
Showing /home/ma/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rails_admin-0.6.5/app/views/rails_admin/main/edit.html.haml where line #2 raised:
undefined method `klass' for #< RailsAdmin::Adapters::ActiveRecord::Property:0x007fd3f99d1e38 >
= rails_admin_form_for #object, url: edit_path(#abstract_model, #object.id), as: #abstract_model.param_key, html: { method: "put", multipart: true, class: "form-horizontal denser", data: { title: #page_name } } do |form|
= form.generate action: :update
Anyone know how to fix that problem ? Also for the dropdown, list of id would be nice, but a perfect solution (I don't know if it's possible) would be to list names for those group.id.

I finally found the solution, which is really stupid, the link was not done because some misstake about the "s" at the end of key words, and the edit must be done on the table, not the id column
here's the correct solution :
indicator.rb:
belongs_to :group # no "s" for belongs_to
rails_admin
...
edit
field :group # table name, not the name of the reference column !
...
indic.rb:
has_many :indics # need "s" for has_many
With this everything works as expected, and the list is with the name by default, so my bonus is also done :)

Related

Lotus Notes: Controlling input on multi-value fields

I have been using multi-value checkbox field. Now I want to disable some of the checkboxes depending on criteria. For example if field A has value "no license" then disable field B's checkbox with value "car" but leave values "bike" and "train" available for user to select.
Having all the values in the same field would make handling the checked values easier but is it possible to modify multi-value field to work like that?
You need to compute the values allowed in the multi-value check box field based on the value of FieldA.
#If(FieldA = "No License"; "Bike":"Train"; "Car":"Bike":"Train")
You then need to set properties on the Checkbox field like this:
And on FieldA like this:
This will refresh the field choices for the checkbox whenever the value in FieldA changes
There is no way to selectively disable single values from checkbox. You can just remove the option completely so that it cannot be selected anymore.
So instead of just typing the values for the checkbox, use a formula to calculate them like:
#If( fieldA = "no license" ;
"bike" : "train" ;
"car" : "bike" : "train" )
The formula could be as complex / simple as you want. e.g. something like:
_licenseNeeded := "car" : "taxi" : "motorbike";
_other := "bike" : "train";
#Trim( #If( fieldA = "no license" ; ""; _licenseNeeded ) : _other )

How to add prefix to `join_through` table in `many_to_many` `assoc` at the Repo.*() level?

I'm trying query a many_to_many relationship with a prefix as follows:
Student
|> join(:left, [s], t in assoc(s, :teachers))
|> Repo.all(prefix: "my_prefix")
which results in a PostgreSQL query:
SELECT s0."id", s0."name", s0."inserted_at", s0."updated_at"
FROM "my_prefix"."students" AS s0
LEFT OUTER JOIN "teachers_students" AS t2 ON t2."student_id" = s0."id"
LEFT OUTER JOIN "my_prefix"."teachers" AS t1 ON t2."teacher_id" = t1."id"
I would expect the prefix to get added to the join_through table teacher_students, but it doesn't get added. Is this a bug in Ecto? Or is there a workaround for this?
Looks like I was able to fix the problem by changing the join_through value in the many_to_many definition from a string to a module name:
schema "students" do
# many_to_many :teachers, Teacher, join_through: "teachers_students", on_replace: :delete
many_to_many :teachers, Teacher, join_through: TeachersStudents, on_replace: :delete
...
end
defmodule MyApp.TeachersStudents do
use Ecto.Schema
alias MyApp.Teachers.Teacher
alias MyApp.Students.Student
schema "teachers_students" do
belongs_to :teacher, Teacher
belongs_to :student, Student
end
end
Ecto ignoring the prefix for the string value may be a bug.

Open XAVA How to Put Default value and Join column of another table

1 .For default value I have tried :
#Required
#Column(name="semester")
#DefaultValueCalculator(value=StringCalculator.class, properties=#PropertyValue(name="value", value="173"))
private String semester;
Getting error :
Error calculating default value
2 . Another Problem is I want to view column of another table into a table.
Table .
- Room [id, name]
- RoomAssigned [id , capacity , type ,status ]
In view/UI , I am showing RoomAssigned which has CRUD .
Now its showing column -
[id,capacity,type,status]
I want to add another column [name] from "Room" table . Here "Room" id is foreign key.
I want to get a view with view / UI :
[id,name,capacity,type,status]
I am new in OpenXava.Thanks in advance.
About the second point, you have to put the reference name in the #View members, use a #ReferenceView in the reference and create a #View in the referenced entity.
Read this documentation first:
http://openxava.wikispaces.com/view_en

Determining Current User for Generic Inquiry

I want to create a number of generic inquiries that lists the leads or other items for the user that's currently logged into Acumatica.
The answer to the question [Current logged in user for GI / Reports in 4.2] (Current logged in user for GI / Reports in 4.2) does not work for me in a generic inquiry. It works as described for reports.
We are using Acumatica 5.20.1757.
Using only PX.Data.AccessInfo in the generic inquiry results in the error: Sequence contains no elements. Using it in a cross join with another table gives: "Invalid object name: AccessInfo. What don't I understand/know?
While the answer above worked quite well, this gets rid of the need to select a name and configure access rights. The following example uses leads.
Provide values for Site Map Title and Site Map Location, found just below the Inquiry Title field/
On the Tables tab, select PX.Data.AccessInfo, PX.Objects.CR.Contact, PX.Object.EP.EPEmployee, PX.SM.LoginTrace, and PX.SM.Users. Accept the defaults for the alias, which will populate once you leave the line.
On the Relations tab:
a) set, Active checked, Parent Table Contact Join Type ** Left **Child Table EPEmployee, Parent Field ownerID Condition Equals Child Field pKID
b) set Active checked, Parent Table Users Join Type ** Inner **Child Table EPEmployee, Parent Field contactID Condition Equals Child Field defContactID
c) set Active checked, Parent Table Users Join Type ** Inner **Child Table LoginTrace, Parent Field username Condition Equals Child Field username
On the Parameters tab set Name ** to Username, **Schema Field to AccessInfo.UserName and **Display Name ** to User Name. Active should not be checked unless you want to check the value of the field.
9 lines were set to meet my specific conditions. Here they are.
a) Active True, Start Brackets (, Data Field Users.IsOnline Condition Equals, From Schema True, Value 1 True, End Brackets , Operator And
b) Active True, Start Brackets , Data Field LoginTrace.ScreenID Condition Equals, From Schema False, Value 1 GI000009 (yours might be different), End Brackets , Operator And
c) Active False, Start Brackets , Data Field LoginTrace.Date Condition Equals, From Schema False, Value 1 Now(), End Brackets ), Operator And
d) Active True, Start Brackets , Data Field Users.UserName Condition Equals, From Schema False, Value 1 =[LoginTrace.Username], End Brackets , Operator And
e) Active False, Start Brackets , Data Field LoginTrace.Username Condition Equals, From Schema False, Value 1 =[EPEmployee.UserID], End Brackets , Operator And
f) Active True, Start Brackets ((, Data Field Contact.ContactType Condition Equals, From Schema FALSE, Value 1 LD, End Brackets , Operator Or
g) Active True, Start Brackets , Data Field Contact.ContactType Condition Equals, From Schema FALSE, Value 1 PN, End Brackets ), Operator And
g) Active True, Start Brackets , Data Field Contact.Status Condition Equals, From Schema True, Value 1 Converted, End Brackets , Operator Or
h) Active True, Start Brackets , Data Field Contact.Status Condition Equals, From Schema True, Value 1 Open, End Brackets ), Operator Or
On the Grouping tab, set Active to True, and Data Field to Contact.ContactID
Sort order, results grid, and Entry point are up to you.
Notes: I found this GI failed in a different query where the LoginTrace.ScreenID condition was left out. It was not tried with left joins for the inner joins, but should have worked.
I just designed a generic inquiry that lists all sales orders created by the user currently logged in for a customer that uses 4.2 of Acumatica. Here are the steps.
At the Tables tab of the Generic Inquiry screen, 2 tables need to be added in addition of the other desired tables (in this example, SO.SOOrder): Data.AccessInfo and SM.Users.
At the Relations tab, the table relation should be created between the desired tables and SM.Users. The Join Type must be Left. Ex.: SOOrder Left Join Users.
The relation must be established using the pKID field of the Users table. Ex.: SOOrder.CreatedByID Equals Users.pKID.
At the Parameters tab, add a line and give it a name like UserName. The Schema Field would then be AccessInfo.UserName. The From Schema option must be activated.
At the Conditions tab, the Data Field Users.UserName Equals the parameter added in #4 as the value.
If you do not want the user to change the default username defined as parameter, make sure to review the access rights of that field in the different roles.

How to reference one foreign key column with multiple primary key column

I am creating BOOK_Issue table which will contain id of person to whom the book is issued.
i have a column name user_id witch will contain ids from tbl_student as well as tbl_faculty. so how to set user_id field of book_issue table with reference to two primary key columns.
Your database schema is not correct.
If you expect unique IDs then they should be in one table.
You can create a table with all the users, and have a column to set their type (student, faculty). Then create 2 different tables for each type that has the proper information for each user based on their type.
Create a "person" superclass that can be either of type "student" or type "faculty". Reference this from the BOOK_Issue table instead.
Basically to create this relationship, you'll need one unique ID that spans both "student" and "faculty". Put this in a table (tbl_person?) and have each row in tbl_student and tbl_faculty reference this new table. It's probably also best to then pull out the fields present in both tbl_student and tbl_faculty and put them in this new supertable instead.
You can solve the problem by either having an extra column in BOOK_Issue table, next to user_id, which indicates if this is a Student ID or a Faculty ID.
Alternatively, the IDs themselves may readily include some pattern which indicate their nature (for example no all faculty Ids may start with say "UC", and none of the student Id are so).
The two solutions above then allow using queries similar to the following
SELECT B.*,
CASE B.BorrowerType -- could be LEFT(user_id, 2) etc...
WHEN 'S' THEN S.Name
WHEN 'F' Then F.Name
END As Name,
CASE B.BorrowerType
WHEN 'S' THEN S.PhoneNumber
WHEN 'F' Then F.Phone -- Note that these constructs allow
-- mapping distinct columns names etc.
END As PhoneNr
FROM BOOK_Issue B
LEFT JOIN tbl_student S ON B.BorrowerType = 'S' AND B.user_id = S.id
LEFT JOIN tbl_faculty F ON B.BorrowerType = 'F' AND B.user_id = F.id
WHERE B.DueDate < '11/23/2009' -- or some other condition
This can get a bit heavy when we need to get multiple columns from the student/faculty tables. A possible alternative is a UNION, but this would then cause the repeating of the search clause.
Finally, the best solution but not avaible on all DBMS is a sub-query driven by an "IF B.BorrowerType = 'S' " condition.
This should be your table design:
FacultyTable (FacultyID, FacultyName)
StudentsTable (StudentID, StudentName, FacultlyID, ...)
BookTable (BookID, BookName, ...)
UsersTable(UserID, UserName, UserPassword, StudentID, LastLogin, ...)
Now this is the main thing:
BookIssedTable(BookIssedID, BookID, UserID)
//This table tells me that a book of "BookID was issued to a user of "UserID"
//this can be better for this is certainly a great improvement from the initial design.

Resources