Richlewis
Richlewis

Reputation: 15374

Create Helper to Concatenate Model Attributes

I may be looking at this all wrong but within my view (Using Active Admin) I would like to concatenate 3 attributes into one, so in my case, forename, middlename and surname into one string, with each part of the name separated by a space

So far i have come up with this

column "Name" do |member|
  member.forename +  member.middlename + member.surname
end

I also thought i could map the results

column "Name" do |member|
  member.map {|m| m.forename,  m.middlename, m.surname }
end

But that throws an error

So a helper would look something like this (as far as i can see it)

def fullname(member)
  member.forename +  member.middlename + member.surname
end

I think im confusing this somewhere along the line as i have 3 attributes I need to pass through the helper dont I?

Any help appreciated

Upvotes: 0

Views: 866

Answers (2)

kddeisz
kddeisz

Reputation: 5182

Two things. First, why don't you put this on the member model. Second, you should use interpolation over concatenation.

def fullname
  "#{self.forename} #{self.middlename} #{self.surname}"
end

Upvotes: 2

Dave Newton
Dave Newton

Reputation: 160181

What three attributes? You have a single member, that has three attributes.

That said: ActiveAdmin provides integration with Draper. Consider using a decorator instead, put fullname in the decorator, then just reference fullname as if it's a model property.

You could also put it in the model if it actually makes sense to do so.

Upvotes: 1

Related Questions