Versioning and polymorphic associations - activerecord

I'm creating a second version of my app using using the following pattern:
class List < ActiveRecord::Base
has_many :listable_items
has_many :lists, through: :listable_items, source: :listable, source_type: 'List'
end
class ListableItem < ActiveRecord::Base
belongs_to :list
belongs_to :listable, polymorphic: true
end
module V2
class List < List
has_many :listable_items
has_many :lists, through: :listable_items, source: :listable, source_type: 'V2::List'
self.inheritance_column = :_non_existing_column
end
end
Module V2
class ListableItem < ListableItem
belongs_to :list, class_name: "V2::List"
belongs_to :listable, polymorphic: true
end
end
list = V2::List.find_by(slug: "people")
=> #<V2::List:0x007fd6e9007f18 id: 97, title: "People"....>
list.listable_items
=> [#<V2::ListableItem:0x007fd6e6497ec8 id: 2633, list_id: 97, listable_id: 100, listable_type: "List",....]
list.listable_items.first.listable
=> #<List:0x007fd6e4185868 id: 100,...>
I assume this is happening because of the class definition in the listable_type column of ListableItems. Is there a way to have it refer to the V2 version of the model instead of the one defined in the db column when I call the associated record?
adding
def listable_type
"V2::" + super
end
to the ListableItem class didn't change the class of the called listable.

The solution was to over-write the listable method, not the listable_type method. There may very well be a better way to do this, but this way worked for me.
Module V2
class ListableItem < ListableItem
belongs_to :list, class_name: "V2::List"
belongs_to :listable, polymorphic: true
def listable
("V2::" + listable_type).constantize.find(listable_id)
end
end
end

Related

Sinatra how to include more than one belongs_to JSON?

I have 3 models
first fight belongs to blue_fighter and red_fighter
class Fight < ActiveRecord::Base
belongs_to :blue_fighter
belongs_to :red_fighter
end
then my other 2 models are like this
blue_fighters
class BlueFighter < ActiveRecord::Base
has_many :fights
has_many :red_fighters, through: :fights
end
red_fighters
class RedFighter < ActiveRecord::Base
has_many :fights
has_many :blue_fighters, through: :fights
end
Now in my application controller
class ApplicationController < Sinatra::Base
set :default_content_type, 'application/json'
get '/fights' do
fights = Fight.all
fights.to_json(include: :blue_fighter)
end
end
im only getting the :blue_fighters i want to include both the :blue_fighter and :red_fighter
i tried something like this it doesnt work
fights.to_json(include: :blue_fighter && :red_fighter)

How is the following Rails model should look like in Sequel?

I've the following two models:
class Dispute < ApplicationRecord
belongs_to :accuser, class_name: 'User', optional: true
belongs_to :defendant, class_name: 'User', optional: true
end
class User < ApplicationRecord
end
Here's the migration for Dispute:
class CreateDisputes < ActiveRecord::Migration[5.0]
def change
create_table :disputes do |t|
t.references :accuser
t.references :defendant
end
end
end
This is how they behave in Rails:
Dispute.first.accuser
# => <# User>
Dispute.first.defendant
# => <# User>
In Sequel, I'm supposed to use many_to_one, but does that mean that Sequel User model should have a corresponding one_to_many? Can't seem to get it to work.
This should work:
Sequel.migration do
change do
create_table(:disputes) do
primary_key :id
foreign_key :accuser_id, :users
foreign_key :defendant_id, :users
end
end
end
class Dispute < Sequel::Model
many_to_one :accuser, :class=>:User
many_to_one :defendant, :class=>:User
end

How to get the association name in rails?

I have the following association code in my user.rb model file
class User < ActiveRecord::Base
has_many :sent_messages, class_name: 'ChatMessage', foreign_key: 'sender_id'
has_many :received_messages, class_name: 'ChatMessage', foreign_key: 'receiver_id'
end
I want a method in the ChatMessage model which should be triggered by the following
current_user.sent_messages
current_user.received_messages
The method should return the name of the association that was called.
Eg:
class ChatMessage < ActiveRecord::Base
after_find :get_association_name
def get_association_name
self.association_name // this should return sent_message or received_message depending on whether current_user.sent_messages or current_user.received_messages was called
end
end
Is there a way to get this association name in rails?
Any help is much appreciated. Thanks
I am not sure, exactly what you are looking for, but
CurrentUser.reflect_on_all_associations(:has_many)
will give an array of all has_many associations.
I haven't used AR associations extensions for cases like this, but you should be able to do:
has_many :sent_messages, class_name: 'ChatMessage', foreign_key: 'sender_id' do
def get_association_name; 'sent_messages'; end
# or, to make this more generic,
# def get_association_name; proxy_association.reflection.name.to_s; end
end
And the method should be accessible from your relation. If you were using Rails 4, you could extract the generic version out into a separate module to extend your associations more succinctly. See http://guides.rubyonrails.org/association_basics.html#association-extensions.
EDIT
Try:
has_many :sent_messages, class_name: 'ChatMessage', foreign_key: 'sender_id' do
def and_set_type
proxy_association.target.each do |msg|
msg.update_attribute(:type, 'sent')
end
scoped
end
end
And then access your sent_messages with current_user.sent_messages.and_set_type.

merge ActiveRecord::Relation with ActiveRecord::Associations

I have 2 models
class User < ActiveRecord::Base
has_many :games
def created_games
Game.where(created_by: self.id)
end
end
class Game < ActiveRecord::Base
belongs_to :user
end
u = User.take
joined_games = u.games
created_games = u.created_games
the variable joined_games is a instance of ActiveRecord::Associations, and created_games is an instance of ActiveRecord::Relation.
Is there any way I can join joined_games and created_games together?
Try adding a scope in User model
scope :joined_games, where(user_id: self.id, created_by: self.id)

rails belongs_to through via association

I'm on rails 3.0 and trying to figure out what would be the proper way to setup a belong_to :through relationship (which) I know is not possible. Here's an example:
class ParentCompany < ActiveRecord::Base
has_many :subsidiaries
has_many :employees, :through => :subsidiaries
end
class Subsidiary < ActiveRecord::Base
belongs_to :parent_company
has_many :employees
end
class Employee < ActiveRecord::Base
belongs_to :subsidiary
belongs_to :parent_company, :through :subsidiary # <-- I know this is invalid
end
I know I can solve it by doing:
class Employee < ActiveRecord::Base
def parent_company
subsidiary.parent_company
end
end
However, I'd like to know if I can do the above via associations.
You can use delegate to accomplish this without using an association
class Employee < ActiveRecord::Base
belongs_to :subsidiary
delegate :parent_company, to: :subsidiary
end

Resources