1

I would like to access the current user's albums by using the primary keys in the Schema below. I have User, Band, And Album models as follows...

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  has_many :bands
end

class Band < ActiveRecord::Base
  has_many :albums
  has_many :users
end

class Album < ActiveRecord::Base
  belongs_to :band
end

Schema as follows...

create_table "albums", force: true do |t|
  t.string   "name"
  t.string   "releaseDate"
  t.string   "artWorkUrl"
  t.integer  "band_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "bands", force: true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "user_id"
end

create_table "users", force: true do |t|
  t.string   "email",                  default: "", null: false
  t.string   "encrypted_password",     default: "", null: false
  t.string   "reset_password_token"
  t.datetime "reset_password_sent_at"
  t.datetime "remember_created_at"
  t.integer  "sign_in_count",          default: 0,  null: false
  t.datetime "current_sign_in_at"
  t.datetime "last_sign_in_at"
  t.string   "current_sign_in_ip"
  t.string   "last_sign_in_ip"
  t.datetime "created_at"
  t.datetime "updated_at"
end

and am trying to use the following method in the Albums controller...

def albums
  @albums = current_user.bands.albums
end

Sorry, i'm sure this is a noob question. I know this should be a simple primary key access through the user --> bands --> albums, using user_id and band_id but have been unable to populate the current users albums through bands. Any insight in much appreciated.

LDro
  • 37
  • 2

3 Answers3

1
def albums
  band_ids = current_user.bands.map(&:id)
  @albums = Album.where(band_id: band_ids)
end

By separating the queries you do not have the n+1 problem: What is SELECT N+1?

Community
  • 1
  • 1
aledalgrande
  • 5,167
  • 3
  • 37
  • 65
1

by using the primary keys

You should be looking at the foreign_keys (primary_keys are when you reference the same table). So what you're really asking should be how do I set up the Rails associations correctly for my models?

Here's how:

#app/models/user.rb
Class User < ActiveRecord::Base
   has_many :bands
end

#app/models/band.rb
Class Band < ActiveRecord::Base
    has_many :albums
    belongs_to :user
end

#app/models/album.rb
Class Album < ActiveRecord::Base
    belongs_to :band
end

This will allow you to call current_user.bands.first.albums

--

Notes

You have to remember the bands association will be a collection. This means you can't just call ....bands.albums. Instead, you'll need to loop through the array like this:

<% for band in current_user.bands do %>
   <%= band.albums.inspect %>
<% end %>
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • You're right - I'm slowly learning (been at ruby/rails for about 6 weeks or so) that I need to think harder about architecting my models/association before jumping into a project. Thanks for taking the time to answer the question. – LDro May 28 '14 at 06:30
  • No problem! Rails is amazing - congrats on sticking with for 6 weeks!! What is your goal with it? – Richard Peck May 28 '14 at 06:31
0

In user class define one more association

class User < ActiveRecord::Base    
  has_many :albums, :through => bands
end

In your controller

@albums = current_user.albums
Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78