Displaying a column of associated names in ActiveAdmin - ruby-on-rails

I have two models, Listing and Invitation, associated with has_and_belongs_to_many. I am looking at invitations through ActiveAdmin and would like to display the names of the associated listings. I attempt this with the following code:
ActiveAdmin.register Invitation do
index do
column("Listings") { |invitation| invitation.listings.each do |listing|
listing.name
end
}
default_actions
end
end
But nothing shows up. How can I get this to work?

I am assuming that you want a single column with all the listings names of the invitation. Please try the following
column 'listing' do |invitation|
invitation.listings.collect(&:name).join(', ')
end
You might want to customize this later on and add a includes(:listings) to increase db retrieval performance.

Related

Rails - ActiveAdmin - Can't get associated modal attribute to show

I am using ActiveAdmin and have an Account model and a User model. Each account will have a has_many relationship with users, and I'd like to be able to list the user names on the account table.
I've seen quite a few SO questions that address the same question and this is how far I've managed to get:
index do
column :id
column :account_status
column :users do |account|
account.users.each do |user|
auto_link user
end
end
column :address do |account|
account.address
end
column :created_at
column :updated_at
actions
end
Note: i've also tried replacing "auto_link user" with "user.name"
This kind of works. It does get the data, but instead of showing attribute values I seem to just get a memory address reference:
I should say here that I am quite new to both Ruby and Rails, so it is likely that I am just missing the point. Considering what I have, is there something I've missed out?
account.users.each will return an array of User objects. That's why you got something like "a memory address reference". To return a list of user.name, you should do something like this:
column :users do |account|
account.users.pluck(:name).join(', ')
end
I use join here to convert an array of names to a string. I'm not sure without join, what will be displayed. You can try it by yourself :)

How to get first and second records from a table having has_many relation in Rails?

I am having a relation where contacts :has_many emails and email :belongs_to contacts.
my contacts.xls.erb file:
class ContactsXLS < BaseXLS
def add_content
contacts.each do |contact|
write_array [contact.title,
contact.updated_at,
contact.email,
contact.phone,
first_email(contact),
second_email(contact) ]
end
end
def first_email(contact)
contact.emails.each do |emm|
emmm.first.email
end
end
def second_email(contact)
contact.emails.each do |emm|
emmm.last.email
end
end
end
I have to add email fields from emails table which belongs for a particular contact in above file.
Here the relation between 2 tables is:
emailable_id in emails table = id in contacts table.
I tried in console as :
c =Contact.find('1wqwqwqw212121')
c.emails.first.email #ex#ex.com
I think we need to write a method and call this array but I couldn't. My requirement is to print first record and second record, please help.
As per the relation contacts :has_many emails,
we can write in array as:
contact.emails.first.try(:email)
likewise for displaying the first record.
You can rewrite those methods
There already have finder methods of first second,..forty_two for rails also you can get any data by using index like find_nth
You can read more on finder methods
First and Second
def first_email(contact)
contact.emails.first.email
end
def second_email(contact)
contact.emails.second.email
end
Then
c =Contact.find('1wqwqwqw212121')
ContactsXLS.new.first_email(c)
ContactsXLS.new.second_email(c)

List all Items from User by its foreign key

I am using ActiveAdmin and i want to list items which belong to a specific user. The two resources have the has_many and belongs_to relationship.
An index pages is listing all the users. Now i would like to render a show block for each user his items.
My show looks now something like this:
ActiveAdmin.register User do
show do
panel "Specific Item List" do
table_for Item.where("user_id=1").fnidi_each do |i|
column("ID"){|item|item.id}
column("Name"){|item|item.name}
end
end
end
end
How do i inherit the user_id from the page to the show panel ? So that each time show is called i can use the users id for the query.
I know these a basic question but my knowledge of Rails/AA is so far quite basic as well ;) Happy for any advice.
Looking at this code from the ActiveAdmin documentation, where "post" seems to be dynamically generated within the register block suggests that in your case you may be able to just do "user.id", etc.
ActiveAdmin.register Post do
show do
h3 post.title
div do
simple_format post.body
end
end
end
So you might try user.items or Item.where(user_id: user.id) instead of your Item.where("user_id=1").

How to show a link to related records in ActiveAdmin?

Let's say I have an Accounts table with an one-to-many relationship with Orders. When viewing a list of Accounts in ActiveAdmin, I'd like to display a link to the related Orders for that Account.
How might I do this? Thanks.
In your admin/order.rb:
ActiveAdmin.register Order do
..
belongs_to :account
..
end
This will give you a route that looks something like this:
admin_account_orders GET /admin/accounts/:account_id/orders(.:format) admin/orders#index
Which you can use, e.g., like this in your admin/account.rb:
ActiveAdmin.register Account do
....
index do
....
column "Orders" do |a|
link_to a.orders.count, admin_account_orders_path(a)
end
....
end
.....
end

how can I get rails_admin to handle associations properly?

Two questions:
1) How can I make a column in the 'list' for a model consist of data from the record's association? In other words, I have a user model and a user has_many posts. I want to simply have a "post count" column in the list. I tried doing:
field :posts do
formatted_value do
value.count
end
end
but that results in a divide by zero error. I even tried doing:
field :posts do
formatted_value do
bindings[:object].posts.count
end
end
but got the same results.
2) How can I filter the listing to a particular scope? For example, I want to make the users post count be a link that is clickable which will show all posts for the given user.
The best I could figure out how to do this was to do:
# note that I created a method post_count to temporarily solve problem #1
field :post_count do
formatted_value do
bindings[:view].link_to value, "/admin/posts?query=#{bindings[:object].id}"
end
end
Which doesn't work very well. Is there a way to instruct rails-admin to do a .where(:user_id => xxx) on the model?
The other thing I wasn't crazy about was having to manually put in 'admin/posts'.. I was trying to see if I could do rails_admin_list_path(:model_name => "posts"). but that didn't seem to work.
You'd probably get a better response on the rails_admin mailing list - http://groups.google.com/group/rails_admin/
For your first question, this should do the trick:
field :posts, :virtual do
formatted_value do
bindings[:object].posts.count
end
end
For your second question, rails_admin now has a filter system - see the "add filter" dropdown at http://demo.railsadmin.org/admin/players . Tapping into that would be a much better method.
rails_admin_list_path(:model_name => "posts") should work, you might have to include Rails.application.routes.url_helpers or similar.
Try adding this to your rails_admin.rb
RailsAdmin.config {|c| c.label_methods << :field_name}
worked for me

Resources