aliasing names in one to many association - ruby-on-rails

I am using some Message model with a content and sender and receiver, so I put
attr_accessible :sender_id
attr_accessible :receiver_id
attr_accessible :content
belongs_to :user
in my User model
has_many :messages, foreign_key: 'sender_id', class_name: "Message"
has_many :pushes, foreign_key: 'receiver_id', class_name: "Message"
First, is this a best practice ?
Btw, suppose I have some message instance, how can I get in a rails way the receiver and the sender instance, without writing User.find(message.sender_id) or `User.find(message.receiver_id). (Btw, I know I can use some table and use has_many through mecanism)

Your user model seems to be setup properly.
Regarding your second question, you can do this:
class Message < ActiveRecord::Base
belongs_to :sender, class_name: 'User'
belongs_to :receiver, class_name: 'User'
end
That way, both message.sender and message.receiver will give you a user object.

You're on the right track.
I have this in my current app:
class Message < ActiveRecord::Base
belongs_to :to, class_name: User
belongs_to :from, class_name: User
end
Given an instance of a Message, I just do, for example:
if message.to == current_user
...
The relevant part of my User class:
class User < ActiveRecord::Base
has_many :messages, foreign_key: :to_id # These are received messages
has_many :sent_messages, foreign_key: :from_id, class_name: Message

Related

Acitverecord specify `inverse_of` with `through` but not the other way

I have 3 models as follows :
class User
has_many :event_series, inverse_of: :user
has_many :events, through: :event_series, inverse_of: :user
end
class EventSeries
belongs_to :user, inverse_of: :event_series
has_many :events, inverse_of: :event_series
end
class Event
belongs_to :event_series, inverse_of: :events
has_one :user, through: :event_series, inverse_of: :events
end
This is all fine.
Now I want to add a special event for each user called the 'showcase_event'.
class User
has_one :showcase_event, class_name: 'Event', inverse_of: :user
end
This isn't working because the Event model doesn't have the user directly, it's associated through EventSeries.
I'm getting an error during serialization:
ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column events.user_id does not exist
I'm using fast jsonapi:
class PublicUserSerializer
include FastJsonapi::ObjectSerializer
...
has_one :showcase_event, record_type: :event, serializer: EventSerializer
...
end
It seems to me that the inverse relationship that I have between User, Event and EventSeries needs to work between User and showcase_event but I don't know how to specify that ONLY the inverse is through EventSeries
Apologies in advance for not having the best vocabulary to describe this problem.
Solved by my rubber duck.
As seen in the documentation :
The #belongs_to association is always used in the model that has the
foreign key.
So I changed my declaration of the showcase_event to this :
class User
belongs_to :showcase_event, class_name: 'Event', inverse_of: :user, optional: true
end
And problem solved

Rails one model two associations

I have a User model and a Task model. All users are the same and each user can create a new task and assign that task to another user. In the Task model I have an assigned_by column and an assigned_to column, so that anyone can create a new task and assign it to anyone else. Later I want to be able for each User to view all tasks assigned to them and all tasks they have assigned to someone else. To do this, I want to setup an association. Is it okay to do something like this?
class Task < ApplicationRecord
belongs_to :user, :foreign_key => 'assigned_by'
belongs_to :user, :foreign_key => 'assigned_to'
end
Where I have two foreign keys in the same model. Then in the User model I have:
class User < ApplicationRecord
has_many :tasks
end
Is this the proper way to do something like this?
What you probably want is to setup three tables:
class User < ApplicationRecord
has_many :assignments, foreign_key: 'assignee_id'
has_many :assignments_as_assigner, foreign_key: 'assignee_id'
has_many :tasks, through: :assignments
has_many :assigned_tasks, through: :assignments_as_assigner
has_many :created_tasks, class_name: 'Task'
foreign_key: 'creator'
end
class Task < ApplicationRecord
belongs_to :creator, class_name: 'User'
has_many :assignments
end
class Assignment < ApplicationRecord
belongs_to :assignee, class_name: 'User'
belongs_to :assigner, class_name: 'User'
belongs_to :task
end
This creates a one to many association so that a task can be assigned to many users.
Each association in the model has to have a unique name - otherwise you will overwrite the previous association.
The approach you suggested will not work as you can't define 2 methods with the same name (in this case both will be called user).
A better way would be calling the relation by what it actually.
For example
class Task < ApplicationRecord
belongs_to :assigned_by, class_name: 'User'
belongs_to :assigned_to, class_name: 'User'
end
You may also need to add a foreign_key option or call the foreign key in the DB assigned_by_id and assigned_to_id
Also, you will need to change your User model as tasks method is ambiguous.
class User < ApplicationRecord
has_many :tasks_delegated, foreign_key: 'assigned_by_id', class_name: 'Task'
has_many :tasks_assigned, foreign_key: 'assigned_to_id', class_name: 'Task'
end
Try to do like this to prevent overwriting.
class User < ApplicationRecord
has_many :owned_tasks, class_name: "Task", foreign_key: "owner_id"
has_many :assigned_tasks, class_name: "Task", foreign_key: "assignee_id"
end
class Task < ApplicationRecord
belongs_to :owner, class_name: "User", foreign_key: "owner_id"
belongs_to :assignee, class_name: "User", foreign_key: "assignee_id"
end

Ownership conditional in a belongs_to association

I have two models: Users and Posts. The way I have things setup, a post belongs to an owner (i.e. user) and also has many participants (i.e. users). In my User model I'd like to ensure that an owner never belongs to a post. I've done this in the front-end but found more code than need-be.
This led me to believe that using conditions would be an ideal solution. I've seen SQL conditions used in this manner but didn't know exactly what the best way to get this done for an ownership scenario. Suggestions?
class User < ActiveRecord::Base
has_many :posts
# belongs_to :posts, conditions: ...
end
class Post
has_many :participants, class_name: "User", foreign_key: "user_id"
belongs_to :owner, class_name: "User", foreign_key: "user_id"
end
To acheive this, I think you need a third model. If you set things up as follows it should work:
User model:
class User < ActiveRecord::Base
has_many :posts # This is the other side of your owner association
has_many :user_posts # This is your link table for participants
has_many :participations, through: :user_posts, source: :user # These are the posts the user is a participant in
end
Post model:
class Post < ActiveRecord::Base
has_many :user_posts, ->(p) { where.not(user_id: p.user_id) } # Here is your condition for the participants
has_many :participants, through: :user_posts, source: :user
belongs_to :owner, class_name: "User", foreign_key: "user_id"
end
UserPost model:
class UserPost < ActiveRecord::Base
belongs_to :user
belongs_to :post
end
As #Oxynum's answer makes clear, you should also think about putting a validation in the UserPost model to prevent the participant from being saved if he is also the owner:
validate :participant_cannot_be_owner
def participant_cannot_be_owner
if user == post.try(:owner)
errors.add(:user_id, "can't be the owner of the post")
end
end
First, there is probably an error in your associations, cause it seems like you need a join table for the participants relationship.
You should probably use a http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
has_many through association.
Something like this :
class User < ActiveRecord::Base
has_one :owned_post, class_name: "Post", foreign_key: :owner_id
has_many :participations
has_many :posts, through: :participations
end
class Participation < ActiveRecord::Base
belongs_to :post
belongs_to :participant, class_name: "User"
end
class Post < ActiveRecord::Base
belongs_to :owner, class_name: "User"
has_many :participants, through: :participations
end
When you have this model, you can use a validation on the participation model to prevent an owner to be a participant. By using a custom validation : http://guides.rubyonrails.org/active_record_validations.html#performing-custom-validations
class Participation < ActiveRecord::Base
belongs_to :post
belongs_to :participant, class_name: "User"
validate :participant_is_not_the_owner
def participant_is_not_the_owner
if participant == post.owner
errors.add(:participant, "can't be the owner")
end
end
end

has_many (different objects) as: one type of object

I need a way of referrencing 2 different objects as 1.
I have a Message object with needs to keep track of Recipients. the problem is that Recipients could be a User or a Contact.
should the models be: ?
class Message < ActiveRecord::Base
has_many :users, as: :recipients
has_many :contacts, as: :recipients
end
class User < ActiveRecord::Base
belongs_to :recipient, polymorphic: true
end
class User < ActiveRecord::Base
belongs_to :recipient, polymorphic: true
end
because, I feel like polymorphic relationships are built to go the opposite way.
also, this way doesn't allow me to reference #message.recipients which is what I need.
I hope this makes sense
Thank you
What you have done is completely incorrect. I think you need many-to-many association. My association whould be that:
class Message < ActiveRecord::Base
has_many :recipient_links
has_many :users, through: :recipient_links, source: :recipient, source_type: 'User'
has_many :contacts, through: :recipient_links, source: :recipient, source_type: 'Contact'
end
class Contact < ActiveRecord::Base
has_many :recipient_links, as: :recipient
has_many :messages, through: :recipient_links
end
class User < ActiveRecord::Base
has_many :recipient_links, as: :recipient
has_many :messages, through: :recipient_links
end
# fields: message_id, recipient_id, recipient_type
class RecipientLink < ActiveRecord::Base
belongs_to :recipient, polymorphic: true
belongs_to :message
end
... I can't add comments yet so i give here solution for: When use answer from up to receive all #message.recipients every type in only 2 request:
RecipientLink.includes(:recipient).where(message_id: #message.id).collect(&:recipient)

Rails: find the relationship name of an instance

Assume we have this relationship:
class Company < ActiveRecord::Base
has_one :company_address, class_name: 'Address', foreign_key: 'company_address_id'
has_one :overseas_address, class_name: 'Address', foreign_key: 'overseas_address_id'
end
If I pry within a new Address instance, is it possible to return :company_address or :overseas_address?
Fields are the same, but I want to do different validations.
Yes, there are alternative ways of solving the problem, but I'm more interested into whether finding the name of the relationship is actually possible.
Thanks!
You should have corresponding belong_to: in your Address model, then try Address.reflect_on_all_associations(belongs_to)
it will return a collection of ActiveRecord::Reflection::AssociationReflection which will have different name (depending on your belong_to)
In order to check instance you should mark your belong_tos as inverse_of: :put_corresponding_association, your has_one as inverse_of belong_tos and then you can check corresponding methods (generated by belong_tos) for presence of parent Company object
The idea is assume an Address as
class Address < ActiveRecord::Base
belongs_to :company, class_name: 'Company', foreign_key: 'company_address_id', inverse_of: :company_address
belongs_to :oversea, class_name: 'Company', foreign_key: 'overseas_address_id', inverse_of: :overseas_address
end
class Company < ActiveRecord::Base
has_one :company_address, class_name: 'Address', foreign_key: 'company_address_id', inverse_of: :company
has_one :overseas_address, class_name: 'Address', foreign_key: 'overseas_address_id', inverse_of: :oversea
end
and you create address as address = company.overseas_address.new
then
address.company == nil
address.oversea == company

Resources