ActiveRecord mapping users to icons

In a simple webapp containing users and user icons, what’s the right
way to use ActiveRecord to store these objects and then designate a
single ‘primary’ icon for each user, given:

  • a set of users.

  • for each user, zero or more icons they’ve uploaded (tied to their
    account).

  • for each user, zero or one icon from their set of uploaded icons
    designated as their “primary” icon.

End of day I want a ‘primary_icon’ attribute in the User class that is
either nil, or references a single icon in the user_icons table.

In the User class I want something logically like:

has_one :primary_icon, :class_name => ‘UserIcon’

where this gets its info from a single add’l column in the users table
that links to the appropriate icon record in the user_icons table, but
can’t figure out how to structure things this way.

Another approach would be to add a flag to the user_icons table to
note for each icon whether it’s the “primary” icon. But this is messy
since we know there will always be at most only one, and now we’ll
have to make sure we always clear all of a user’s other icon primary
flags when setting a new one, and there is risk of things getting into
a bad state where a user has more than one primary icon marked in the
database.

Yet another approach is to have another table, e.g. users_user_icons
(or user_icons_users), mapping the user id to the appropriate
user_icon id. Not sure how to structure this with ActiveRecord
statements either.

It’s confusing how to structure this, with my head not yet fully
wrapped around ActiveRecord as this is my first RoR test app
toe-dipping, since normally you’d just set up a link from the user to
the user_icons but in this case we already have one that has MULTIPLE
entries per user as a user can have multiple icons – so we need
either a single add’l column in the users table, or a separate table,
and I can’t see how to inform ActiveRecord or structure the tables or
both so that it’s clear either of those are the desired intent.

Below is more detail, appreciate any thoughts.

TABLES

CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
username VARCHAR(32) NOT NULL,
/* main thought is to add another icon_id key column here
that maps directly to the single primary icon or is null if
there is none, but can’t seem to get AR to use this */
PRIMARY KEY (id)
) type=InnoDB CHARACTER SET utf8;

CREATE TABLE user_icons (
id INT NOT NULL AUTO_INCREMENT,
user_id INT NOT NULL,
name VARCHAR(255),
content_type VARCHAR(255),
data blob,
PRIMARY KEY (id)
) type=InnoDB CHARACTER SET utf8;

CLASSES

class User < ActiveRecord::Base
has_many :icons, :class_name => ‘UserIcon’

below is logical thought but it doesn’t appear to update any

columns in db when set and user is then saved

has_one :primary_icon, :class_name => ‘UserIcon’
end

class UserIcon < ActiveRecord::Base
belongs_to :user
end

Thanks,

  • Walter

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs