Facebook like requests model. Many-to-many with the same model - ruby-on-rails

I have a Users model:
class User < ActiveRecord::Base
has_many :sent_requests, :foreign_key => :sender_id, :class_name => "Request"
has_many :recieved_requests, :foreign_key => :recipient_id, :class_name => "Request"
and a requests model:
class Request < ActiveRecord::Base
belongs_to :sender, class_name: "User", :source => "sender_id"
belongs_to :recipient, class_name: "User", :source => "recipient_id"
Im struggling to understand the difference of :foreign_key, :through, :source, :class_name, etc.
This seemed to be slightly different from most tutorial examples because a User can belong to a relationship on both sides (although not at the same time).
Any advice for how to get this working. And more importantly how to think about the relationship?
fyi:
my Request model just has sender_id, recipient_id, and accepted:boolean.

Here's what I'm using:
In user's model:
has_many :sent_requests,
:class_name => "Request",
:foreign_key => "sender_id"
has_many :received_requests,
:class_name => "Request",
:foreign_key => "receiver_id"
In request's model:
belongs_to :sender,
:class_name => "User"
belongs_to :receiver,
:class_name => "User"
Note that we user :class_name to specify the name of the class when rails cannot infer the name of the class. If you say:
has_many :requests
then you don't need to include the class name. Rails will automatically know that it is Request. But if you want to rename :request to something like :sent_request, you need to specify the class.
Also note that the foreign key is by default guessed to be the name of the association with an “_id” suffix. So a class that defines a belongs_to :person association will use “person_id” as the default :foreign_key. You need to specify the foreign_key if you are using a name that does not satisfy rails naming convention.

Related

Mongoid could not determine the inverse foreign key to set

I'm building a simple ticket system in an application using MongoDB. At one point, I was able to create tickets, but now I am not. The User model is as follows:
class User
include Mongoid::Document
include Mongoid::Timestamps::Updated
has_many :initiated_tickets, :class_name => 'Ticket', :inverse_of => :initiator
has_many :assigned_tickets, :class_name => 'Ticket', :inverse_of => :assignee
The Ticket model is as follows:
class Ticket
include Mongoid::Document
include Mongoid::Timestamps::Updated
field :name
field :initiator_email
field :assignee_email
field :comment
belongs_to :alert
has_one :initiator, :class_name => 'User', :inverse_of => :initiated_tickets
belongs_to :assignee, :class_name => 'User', :inverse_of => :assigned_tickets
When I attempt to create a ticket, I get an error from Mongoid stating:
Mongoid::Errors::InverseNotFound:
Problem:
When adding a(n) User to Ticket#initiator, Mongoid could not determine the inverse foreign key to set. The attempted key was 'initiated_tickets_id'.
I'm not sure what's going wrong here. It looks like the inverse_of is set up correctly for both. Any idea why this isn't working, when it previously was? Thanks!
You only need inverse_of defined on the belongs_to side. Whereas, the has_many side should have the foreign_key defined. I have the exact same relation working as follows:
class User
has_many :initiated_tickets, foreign_key: "initiator_id", class_name: "Ticket"
has_many :assigned_tickets, foreign_key: "assignee_id", class_name: "Ticket"
class Ticket
field :initiator_id, :type => String
field :assignee_id, :type => String
belongs_to :initiator, inverse_of: "initiated_tickets" class_name: "User"
belongs_to :assignee, inverse_of: "assigned_tickets" class_name: "User"
EDIT
Rewrote my answer because I was mistaken originally.

How to << a Join Model with attributes to a collection

I have a User model which joins to another User model through a UserRelationship join table. UserRelationships have an attribute (approved/pending/revoked) which must be set, but does not default to any option. There are two associations which reflect this, traveler and delegate, so my models look like this:
User.rb
has_many :traveler_relationships, :class_name => 'UserRelationship', :foreign_key => :delegate_id
has_many :travelers, :class_name => 'User', :through => :traveler_relationships
has_many :delegate_relationships, :class_name => 'UserRelationship', :foreign_key => :user_id
has_many :delegates, :class_name => 'User', :through => :delegate_relationships
has_many :buddy_relationships, class_name: 'UserRelationship', foreign_key: :user_id
has_many :buddies, class_name: 'User', through: :buddy_relationships, source: :delegate
UserRelationship.rb
belongs_to :relationship_status
belongs_to :traveler, :class_name => 'User', :foreign_key => 'user_id'
belongs_to :delegate, :class_name => 'User'
PENDING = 1
CONFIRMED = 3
REVOKED = 5
I'm trying to write up some specs where one user is related to another and the simplest way to write it would be #user1.travelers << #user2 but this fails the database constraint that UserRelationship.relationship_status not be null.
When I try #user1.buddies.create(delegate: #user2, relationship_status: RelationshipStatus::CONFIRMED), it fails saying UnknownAttributeError on delegate. I looked at this question and tried its solution, using attr_acessible, but it didn't change the UnknownAttributeError.
What is the way to create this join record with an attribute set?
I assume UserRelationship has a foreign_key delegate_id for the delegate
class User < ActiveRecord::Base
has_many :traveler_relationships, :class_name => 'UserRelationship', :foreign_key => :user_id
has_many :delegate_relationships, :class_name => 'UserRelationship', :foreign_key => :delegate_id
has_many :travelers, :class_name => 'User', :through => :traveler_relationships
has_many :delegates, :class_name => 'User', :through => :delegate_relationships
end
class UserRelationship < ActiveRecord::Base
belongs_to :traveler, :class_name => 'User', :foreign_key => :user_id
belongs_to :delegate, :class_name => 'User', :foreign_key => :delegate_id
PENDING = 1
CONFIRMED = 3
REVOKED = 5
end
I dont think you can use the shortcut #user1.delegates << #user2 since you need to specify relationship status. Try:
#user1.traveler_relationships.create(delegate: #user2, relationship_status: RelationshipStatus::CONFIRMED)
I've excluded the buddy synonym here. It's complex enough as is. When this works you can look into adding synonyms.
You're on the right track when you try to create from buddies (I'm assuming that's your join model relation). The problem is that your column name is relationship_status and you were passing status to create. Try this:
#user1.buddies.create(delegate: #user2, relationship_status: RelationshipStatus::CONFIRMED

Rails multiple associations between two models

I have Flight, Person, and Glider models in a Rails 3 app. I've defined custom relationships because I need more than one foreign key referencing a Person from the flights table. Associations work but ONE-WAY only.
class Flight < ActiveRecord::Base
belongs_to :pilot, :class_name => "Person"
belongs_to :instructor, :class_name => "Person"
belongs_to :towplane_pilot, :class_name => "Person"
belongs_to :airplane_instructor, :class_name => "Person"
belongs_to :glider
belongs_to :rep_glider, :class_name => "Glider"
belongs_to :departure_airfield, :class_name => "Airfield"
belongs_to :arrival_airfield, :class_name => "Airfield"
end
class Glider < Aircraft
has_many :flights
has_many :replaced_flights, :foreign_key => "rep_glider_id", :class_name => "Flight"
end
class Person < ActiveRecord::Base
has_many :flights, :foreign_key => "pilot_id", :class_name => "Flight"
has_many :instructed_flights, :foreign_key => "instructor_id", :class_name => "Flight"
has_many :towed_flights, :foreign_key => "towplane_pilot_id", :class_name => "Flight"
has_many :instructed_towing_flights, :foreign_key => "airplane_instructor_id", :class_name => "Flight"
end
####What works#####
Flight.first.glider
Flight.first.rep_glider
Flight.first.pilot
Flight.first.instructor
Flight.first.towplane_pilot
Flight.first.airplane_instructor
Glider.first.flights
Glider.first.replaced_flights
####What doesn't work#### ----> NoMEthodError 'match'
Person.first.flights
Person.first.instructed_flights
Person.first.towed_flights.
Person.first.instructed_towing_flights
I'm almost there, but I don't understand how Glider.first.flights does work when Person.first.flights doesn't.
UPDATE: Associations with 'Airfield' works... so I'm clueless as to why it doesn't work with 'Person'
class Airfield < ActiveRecord::Base
has_many :takeoff_flights, :foreign_key => "departure_airfield_id", :class_name => "Flight"
has_many :grounded_flights, :foreign_key => "arrival_airfield_id", :class_name => "Flight"
end
###Works Correctly
Airfield.first.takeoff_flights
Airfield.first.grounded_flights
Flight.first.departure_airfield
Flight.first.arrival_airfield
Do your pilots have types? like a pilot_type column? I just also started reading into these kinds of patterns and luckily it's still a bit fresh(hopefully. please correct me if Im wron rails ninjas! :))
You are in need of the polymorphic pattern as discussed here:
http://asciicasts.com/episodes/154-polymorphic-association
I've been told that the association between these models is set correctly.
I added a new record to the flights table, and now the associations work correctly with this new record and all the previous ones. I'm not really sure how it is working now, but it sure does.

Rails multiple has_one associations

I have multiple models with created_by and modified_by columns. This is what I have for a Deal Model.
class Deal
has_one :user , :foreign_key => 'created_by'
has_one :user , :foreign_key => 'modified_by'
end
class User
belongs_to :created_by , :class_name => 'Deal' , :foreign_key => 'created_by'
belongs_to :modified_by , :class_name => 'Deal' , :foreign_key => 'modified_by'
end
When I create the deal, looks like it is saving correctly. But in the show view when I try to get #deal.created_by.email I get an "undefined method email" error. Can some tell me how to get this working please?
Also since I have multiple models with these two columns, there can be many belongs_to in User model. Is there an elegant solution for this case?
First thing you have to add is the specification of accessible attributes.
In User you would have to add:
attr_accessible :email, :created_by, :modified_by
In Deal:
attr_accessible :created_by, :modified_by
But you should also change the direction of your relation. The foreign_key is always on the belongs_to side.
This is what worked for me:
class Deal < ActiveRecord::Base
belongs_to :created_by, :class_name => "User", :foreign_key => "created_by"
belongs_to :modified_by, :class_name => "User", :foreign_key =>"modified_by"
attr_accessible :created_by, :modified_by, :name
end
class User < ActiveRecord::Base
has_many :created_deals, :class_name => "Deal", :foreign_key => "created_by"
has_many :modified_deals, :class_name => "Deal", :foreign_key => "modified_by"
attr_accessible :created_deals, :modified_deals, :name
end
If you have more models, which look similiar you could probably use polymorphic associations: http://guides.rubyonrails.org/association_basics.html#polymorphic-associations
First of all, from my experience it is generally a bad idea to have associations using the foreign key as name. Especially when writing fixtures it seems rails will get confused between setting the actual value "created_by" or the model in the created_by association. In my models I generally use these associations for the cases you describe:
belongs_to :creator, :class_name => "User", :foreign_key => 'created_by'
belongs_to :modifier, :class_name => "User", :foreign_key => 'modified_by'
You can use association names like 'creating_user' instead if you prefer. If you really want created_by as association name you should have created_by_id or something similar as foreign key, just as long as its not equal to the association name.
Then I am a bit confused by your pasted code. Your choice "Deal has_one User" and "User belongs_to Deal" means that the users table will have the columns created_by and modified_by (foreign keys) containing Deal Ids, basically meaning that users get created by a single deal? However it seems like deals should get created by users and not the other way round. Your example of deal.created_by.email can not work at all with your associations, since deal would not have an association called "created_by", only "user", of which you have two associations with the same name in a single model which can not work at all in the first place.
Fixing your associations similar to what Patrick suggested:
class Deal < ActiveRecord::Base
belongs_to :creator, :class_name => "User", :foreign_key => "created_by"
belongs_to :modifier, :class_name => "User", :foreign_key =>"modified_by"
end
class User < ActiveRecord::Base
has_many :created_deals, :class_name => "Deal", :foreign_key => "created_by"
has_many :modified_deals, :class_name => "Deal", :foreign_key => "modified_by"
end

rails way user to user messaging, do I need a join table?

Quick question (I think). I have users, and I would like to allow them to send messages to one another. My question is, should I have a user table, a message table, and a users to messages join table, or should I just store to_user_id and from_user_id in the messages table. If the latter, what would the association look like for that? Can you even reference a 'local' key for an association?
You can do that with a couple of simple has_many associations. Since it's self-referential, you'll need to override some of the Rails magic to make it work.
class User < ActiveRecord::Base
has_many :sent_messages, :class_name => 'Message', :foreign_key => 'sender_id'
has_many :received_messages, :class_name => 'Message', :foreign_key => 'recipient_id'
end
class Message < ActiveRecord::Base
belongs_to :sender, :class_name => 'User'
belongs_to :recipient, :class_name => 'User'
end
Rails doesn't have a cleaner way to doing self-referential associations that I know of.
I think the latter sounds fine. This is just off the top of my head but I know AR's associations have options like this...
class Message < ActiveRecord::Base
belongs_to :sender, :class_name => :user, :foreign_key => :from_user_id
belongs_to :recipient, :class_name => :user, :foreign_key => :to_user_id
#...
end
class User < ActiveRecord::Base
has_many :received_messages, :class_name => :message, :foreign_key => :to_user_id
has_many :sent_messages, :class_name => :message, :foreign_key => :from_user_id
end

Resources