Ruby on Rails: Two References with Different Name to the Same Model

Ruby on Rails: two references with different name to the same model

You can specify the class name of the association, doc

class Message < ActiveRecord::Base
belongs_to :sender, class_name: 'User'
belongs_to :recipient, class_name: 'User'
end

Referencing the same model twice in Rails with a simple entity?

You can't actually use a single assocation here as ActiveRecord does not support assocations where the foreign key could be in one of two columns. Each has_one/has_many corresponds to a single foreign key column on the inverse side.

Instead you need one assocation on Address for each foreign key on Shipment:

class Shipment < ApplicationRecord
belongs_to :address_from,
class_name: 'Address',
inverse_of: :outgoing_shipments
belongs_to :address_to,
class_name: 'Address',
inverse_of: :incoming_shipments
end

class Address < ApplicationRecord
has_many :outgoing_shipments,
class_name: 'Shipment',
foreign_key: :address_from_id,
inverse_of: :address_from
has_many :incoming_shipments,
class_name: 'Shipment',
foreign_key: :address_to_id,
inverse_of: :address_to
end

While you could use has_one here you should note that there is nothing preventing a address from having multiple shipments unless you add uniqueness constraints on shipments.address_from_id and shipments.address_to_id and validations. Not sure why you would want this though.

Ruby on rails - Reference the same model twice?

Add this to your Model

belongs_to :sender, :class_name => "User"
belongs_to :recipient, :class_name => "User"

And you are able to call @message.sender and @message.recipient and both reference to the User model.

Instead of user:references in your generate command, you'd need sender:references and recipient:references

Rails: Multiple references to the same model error

Rails guesses based on the association, because you're referencing a table it can't determine from the association you'll have to add it yourself.

class CreateMessages < ActiveRecord::Migration
def change
create_table :messages do |t|
t.references :user, index: true, foreign_key: true
t.references :teammember, index: true
t.text :body
t.boolean :read, default: false

t.timestamps null: false
end
add_foreign_key :messages, :users, column: :teammember_id
end
end

Two references of the same model in another Rails: 4

you are trying to achieve many to many association, I would suggest you to use has_many_through association. you can read about it over here.

your tables will be like:

User:

id, email, name and other fields related to user

Box:

id, name and Fields related to boxes

User_Boxes: (Join table for mapping users with boxes, to do what you're trying to achieve)

id, user_id, box_id

so,
according to your diagram:

let the id's of boxes in box table be:

  1. box home
  2. box 2
  3. box closet
  4. box keys

Your mapping table would look like:

id   box_id   user_id

1 1 1
2 1 3
3 2 5
4 2 9
5 3 1
6 3 9
7 4 7
8 4 2

I hope this will solve your problem.

Defining two references to the same column in another table

A database table cannot have two columns with the same name. This is what you'll need in order to get this to work. (I'm going to use home_team and away_team to help distinguish them, but obviously you could use team1 and team2.)

rails g migration AddTeamsToMatch home_team_id:integer away_team_id:integer

This will generate a migration that looks like this:

class AddTeamsToMatch < ActiveRecord::Migration
def change
add_column :matches, :home_team_id, :integer
add_column :matches, :away_team_id, :integer
end
end

Then in your Team model, you can have the following:

class Team < ActiveRecord::Base
has_many :home_matches, class_name: "Match", foreign_key: "home_team_id"
has_many :away_matches, class_name: "Match", foreign_key: "away_team_id"

# Get all matches
def matches
self.home_matches + self.away_matches
end
end

In your Match model, you can have:

class Match < ActiveRecord::Base
belongs_to :home_team, class_name: "Team"
belongs_to :away_team, class_name: "Team"

# List both teams as array
def teams
[self.home_team, self.away_team]
end
end

Hopefully this helps.

Rails reference 2 columns from another model

This first argument after the has_many needs to be different.

Something like this:

has_many :child_organization_organizations, class_name: "ChildOrganization", foreign_key: "organization_id", dependent: :destroy
has_many :child_organization_child_organizations, class_name: "ChildOrganization", foreign_key: "child_organization_id", dependent: :destroy

How do I add migration with multiple references to the same model in one table? Ruby/Rails

You can do this simply with the add_column method in your migrations and set up the proper associations in your classes:

class AddFields < ActiveRecord::Migration
def change
add_column :tickets, :image_1_id, :integer
add_column :tickets, :image_2_id, :integer
end
end

class Ticket < ActiveRecord::Base
belongs_to :image_1, :class_name => "Image"
belongs_to :image_2, :class_name => "Image"
end

class Image < ActiveRecord::Base
has_many :primary_tickets, :class_name => "Ticket", :foreign_key => "image_1_id"
has_many :secondary_tickets, :class_name => "Ticket", :foreign_key => "image_2_id"
end

This blog post, Creating Multiple Associations with the Same Table, goes into more detail.



Related Topics



Leave a reply



Submit