I'm working on an app with a friend type system. I currently have
setup like this with 2 tables.
users table keeps track of users
user_relationships table keeps track of friendships. Now, there
should be two rows for each relationship. This is to keep track of
the directional nature of the relationship between users. Each time a
relationship is made, both rows should be inserted into the table.
user_1 user_2 data
user_2 user_1 data
So what I can't decide is what is the best way to implement these in
my rails app to be able to create a view of all user_relationships
with a given userid.
I can define a Relationship class, and mark User with has_many. But
that will only select the relationships based on one column right? So
it would look in one column and find all rows from that column. so if
I call User.relationships I will only have half of the relationship
info right?
So what I think i'm looking for is a way to overload the
User.relationships so that I can return a list of Relationships that I
compiled by making two queries, one on each column?
I can think of some other options that might work in this instance
(since i know there is always two rows of data I can just query on 1
column, and then in the Relationship model handle the dual column
nature of info), but I think it would still be interesting to know how
to overload the function, or describe this behavior better in my
model.
I'm working on an app with a friend type system. I currently have
setup like this with 2 tables.
users table keeps track of users
user_relationships table keeps track of friendships. Now, there
should be two rows for each relationship. This is to keep track of
the directional nature of the relationship between users. Each time a
relationship is made, both rows should be inserted into the table.
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.timestamps
end
end
def self.down
drop_table :users
end
end
class CreateRelations < ActiveRecord::Migration
def self.up
create_table :relations do |t|
t.integer :from_user
t.integer :to_user
# other data specific to relation
t.timestamps
end
end