Confused about My Model -- Inconsistent Attributes

Hi! I’m developing a music sharing website. I have an album model that I want to contain a list of Tracks. I’m running into an issue where I don’t know if the data exists or is connected to the model or?

When I show an album, it gives me:

#<Album:0x000000010b62cb20
 id: nil,
 title: "Take Me Apart",
 release_date: "2017-10-06",
 created_at: nil,
 updated_at: nil,
 artist_id: nil,
 spotify_id: "4mQ6UTM71F02O1jMVi625N">

It seems there are no tracks right? However, when I use @album.tracks, it gives me:

[#<Track:0x000000010bd343a0
  id: nil,
  title: "Frontline",
  link: "https://open.spotify.com/track/51QgyO73gGeEIpG5SqoA6n",
  artist_id: nil,
  album_id: nil,
  track_num: 1,
  created_at: nil,
  updated_at: nil,
  spotify_id: "51QgyO73gGeEIpG5SqoA6n",
  preview_link: nil>,
 #<Track:0x000000010997c598
  id: nil,.......

and so on.

Technically this is kinda working how it should, but I don’t want to rely on something that I don’t understand for such a crucial aspect.

Thanks

Although you don’t say how you produced this output, it looks like the result of Object#inspect, which will display the attributes of the base object but not any associations. This is normal behavior.

1 Like

How I produced it was using rails console setting @album = Album.first and typing @album gave me the first output. After that I used @album.tracks and it gave me the second output.

This answer sounds right. Is there a command to show attributes as well as associations?

That requires some introspection. See: ActiveRecord::Reflection::ClassMethods

I understand now.

Thank you for the help!

1 Like

Basically your relation is declared in the model class, probably in ‘app/models/album.rb’, and it should have something like:

has_many :tracks, dependent: :destroy

That is what indicates the relation in your model, but the tracks per se are not store in an attribute of the class. You will not find a field tracks in the db table of your Album model (schema.rb)