Git Product home page Git Product logo

Comments (14)

slenderock avatar slenderock commented on May 18, 2024 2

and then you can use:

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

from trestle.

spohlenz avatar spohlenz commented on May 18, 2024 2

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.

from trestle.

slenderock avatar slenderock commented on May 18, 2024

Try this:

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

from trestle.

zippax avatar zippax commented on May 18, 2024

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

from trestle.

slenderock avatar slenderock commented on May 18, 2024

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)

from trestle.

zippax avatar zippax commented on May 18, 2024

Error
undefined method 'parent'

if I change it to :parent_id will show me :

undefined method 'name' for 1:Fixnum

from trestle.

slenderock avatar slenderock commented on May 18, 2024

undefined method 'parent'

for what class ?

from trestle.

zippax avatar zippax commented on May 18, 2024

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

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

from trestle.

slenderock avatar slenderock commented on May 18, 2024

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 }

from trestle.

zippax avatar zippax commented on May 18, 2024

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)

from trestle.

zippax avatar zippax commented on May 18, 2024

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

from trestle.

slenderock avatar slenderock commented on May 18, 2024

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?

from trestle.

zippax avatar zippax commented on May 18, 2024

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

from trestle.

slenderock avatar slenderock commented on May 18, 2024

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

from trestle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.