ruby on rails - Extracting data from an Activerecord with joined tables -


project.joins(:project_status).where(id: 1).first 

i want use string such "project.projectstatus.name" , return name of joined project status, , project.name.

.read_attribute() doesn't seem go deeper joined/included records. .send works when .send('projectstatus').send('id') not seem ideal , dangerous, these variables come users.

it's templating engine might have like.. "{{project.name}} status changed {{projectstatus.name}}"

are there gems can pick up? i'm exhausting i've been searching in google now. in cakephp have used hash::extract before little xpath.

what association between project , projectstatus here? assume it's has_one: :project_status?

anyway, if want able call @project.name ( @project instance of class project; .name should specific each individual project, should define instance method rather class method project.name), can create method in project model:

def name   project_status.name end 

assuming defined association in model as

has_one: :project_status 

now name can retrieved using .name


Comments