John
John

Reputation: 634

Select a row from table and diplay name

I have two entities, Project and Domain. Project has_many Domains and Domain belongs_to Project, but the domain_id is stored into Project table.

In rails c I tried

p = Project.first d = Domain.where(id: p.domain_id)

And I got

Domain Load (0.2ms) SELECT "domains".* FROM "domains" WHERE "domains"."id" = 1 => #<ActiveRecord::Relation [#<Domain id: 1, name: "alex.com", created_at: ...">]>

When I tried

d.name => the result was 'Domain' instead of alex.com

2.0.0-p247 :007 > d.name => "Domain"

Why is this happening?

Thank you

Upvotes: 1

Views: 66

Answers (1)

phoet
phoet

Reputation: 18845

it's a relation ActiveRecord::Relation so a transparent array of results. try calling first on it to get to the actual object.

Upvotes: 2

Related Questions