Topic: Association help

Hey guys,

I'm altering my personal website to alter how my galleries work. Currently I have an albums page which just lists all my albums and each album has a number of photos.

I am now adding collections to the mix.

- Albums live in collections
- A collection can house other collections but not other collections and albums at the same level
- Never more than 3 collection levels deep so c1 > c2 > c3 >a1
- 1 album can only belong to 1 collection at a time

On each of my albums I have the id of it's parent collection and the relationships defined are:

- Collection has_many Albums
- Album belongs_to Collection

now, Where I am getting confused is that a collection can belong to another collection or can be a top level (root) collection and how to specify this relationship.

Any ideas on what I should do here?

Kind regards,
Neil

Re: Association help

Any thoughts on this anyone?

Neil

Re: Association help

Not sure to get you right, but you can try to apply a Twitter-like relation, i.e. you have one table to contain Users, one table for relations. As you see you have one only table to contain Users, but each user can have followers(be followed) and can follow someone (following). All this is implemented just by using some tricks in User model:

class User < AR
  has_many :relationships, foreign_key: "follower_id", dependent: :destroy
  has_many :followed_users, through: :relationships, source: :followed  

  has_many :reverse_relationships, foreign_key: "followed_id",
                                   class_name:  "Relationship",
                                   dependent:   :destroy

  has_many :followers, through: :reverse_relationships, source: :follower
end

class Relationship < ActiveRecord::Base
  attr_accessible :followed_id
  
  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"
  
  validates :follower_id, presence: true
  validates :followed_id, presence: true
  
end

Here is the DB schema for relations table:

create_table "relationships", :force => true do |t|
    t.integer  "follower_id"
    t.integer  "followed_id"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end

  add_index "relationships", ["followed_id"], :name => "index_relationships_on_followed_id"
  add_index "relationships", ["follower_id", "followed_id"], :name => "index_relationships_on_follower_id_and_followed_id", :unique => true
  add_index "relationships", ["follower_id"], :name => "index_relationships_on_follower_id"

Re: Association help

Hey,

Thanks for that!

It does sort of make sense but I just tried to convert it to fit with my own scenario but am getting confused with how followers and following map to my Collections

Any thoughts?

Also, once I have the relationship done I need to figure out how to pull that data out to display it. I want my main collections page to list all collections which are top level, when one of those is clicked it should then show all collections who are descendents of the collection clicked. Until no more children of the collection exists and the albums within are shown.

Wow, I do make live difficult for myself don't I!

Any preliminary ideas on how I can do it?

Kind regards,
Neil