AntonAL
AntonAL

Reputation: 17430

Get attribute of ActiveRecord by name

I have a partial, that will be used in many places of a project.

It renders items, related to specified tag, like:

@tag.articles.each do |a|
    # render articles
end

I need this partial to render not only articles, but any other items, associated with tags.

So, this partial has one parameter association_name and looks like this:

@tag[association_name].each do |a|
    # render articles
end

I call this partial in following ways:

# to render articles
render :partial => "items", :locals => {:association_name => "articles"}

# to render videos
render :partial => "items", :locals => {:association_name => "videos"}

# etc.

The problem is - i cannot refer fields of Article model in a way:

@article[association_name]

How to do it and why it does't works ?

Upvotes: 3

Views: 1687

Answers (1)

Dogbert
Dogbert

Reputation: 222448

@article.send(association_name)

Upvotes: 5

Related Questions