Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch column parent :name instead of :parent_id #63

Closed
zippax opened this issue Jan 6, 2018 · 14 comments
Closed

fetch column parent :name instead of :parent_id #63

zippax opened this issue Jan 6, 2018 · 14 comments

Comments

@zippax
Copy link

zippax commented Jan 6, 2018

table do
    column :name
    column :parent_id
    actions
end

the second column will show the id. I want to show parent name instead of the id.

@slenderock
Copy link

Try this:

column :parent, ->(parent) { parent.name }
or 
column :parent, &:name

@zippax
Copy link
Author

zippax commented Jan 6, 2018

Hi @slenderock this will show me the current child name, I need to show the parent name

@slenderock
Copy link

slenderock commented Jan 6, 2018

Hello, @zippax
Sorry for that. I made a little typo

column :parent, ->(current_record) { current_record.parent&.name }

I guess "&" is required to prevent errors when parent doesn't exist for this record.
Hope this helps)

@zippax
Copy link
Author

zippax commented Jan 6, 2018

Error
undefined method 'parent'

if I change it to :parent_id will show me :

undefined method 'name' for 1:Fixnum

@slenderock
Copy link

undefined method 'parent'

for what class ?

@zippax
Copy link
Author

zippax commented Jan 6, 2018

column :parent, ->(current_record) { current_record.parent_id&.name }

ActionView::Template::Error at /categories
undefined method `name' for 1:Fixnum

@slenderock
Copy link

slenderock commented Jan 6, 2018

You should use active_record object(not id) if you want to get method #name.
So you need to change back #parent_id to #parent

If you get "undefined method 'parent'" you probably forgot to set belongs_to association in your model.
Please check this out.
It will be great if u can share github link where this problem produses

Btw, I have this line of code in my project and it works fine:

column :user, ->(current_record) { current_record.user.full_name }

@zippax
Copy link
Author

zippax commented Jan 6, 2018

Actually the Category model have name and parent_id so there is no association between 2 models. both are in the same table

=> Category(id: integer, name: string, parent_id: integer, created_at: datetime, updated_at: datetime)

@zippax
Copy link
Author

zippax commented Jan 6, 2018

this is a Github repo https://github.com/zippax/cs

@slenderock
Copy link

Seems like you don't have parent model at all. So how you want to get parent name, if you don't have this relation?

@zippax
Copy link
Author

zippax commented Jan 6, 2018

so I have to make subCategory model and make association between it and the Category model

@slenderock
Copy link

slenderock commented Jan 6, 2018

You should have this code for subcategory

  has_many :subcategories, class_name: 'Category', foreign_key: :parent_id
  belongs_to :parent, class_name: 'Category', inverse_of: :subcategories, optional: true

@slenderock
Copy link

slenderock commented Jan 6, 2018

and then you can use:

column :parent, ->(category) { category.parent&.name }

@spohlenz
Copy link
Member

spohlenz commented Jan 8, 2018

As @slenderock mentions, having the associations in place are the key to getting this working.

One thing I would add is that in many cases, you can drop the custom block all together and just use:

column :parent

This will automatically display the parent instance (if present) by finding a method from Trestle.config.display_methods that it responds to. This defaults to [:display_name, :full_name, :name, :title, :username, :login, :email] so you won't need to alter anything when using the name method.

It will also automatically link the name to a corresponding admin for the parent model, if it exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants