Rails Belongs_To Association (With :Class_Name) Returns Nil

Rails belongs_to association (with :class_name) returns nil

You need to specify both :class_name and :foreign_key, for example:

belongs_to :admin, :class_name => "User", :foreign_key => "user_id"

Rails belongs_to association (with :class_name) returns nil

You need to specify both :class_name and :foreign_key, for example:

belongs_to :admin, :class_name => "User", :foreign_key => "user_id"

Rails I can't use belongs_to and class name methods correctly

I solved the problem by creating the Room model like this:

class CreateRooms < ActiveRecord::Migration[6.1]
def change
create_table :rooms do |t|
t.text :rules
t.boolean :online
t.integer :room_id
t.string :password
t.belongs_to :winner, null: false# remove,foreign_key:true
t.belongs_to :creator, null: false# remove,foreign_key:true

t.timestamps
end
add_foreign_key :rooms, :users, column: :winner_id
add_foreign_key :rooms, :users, column: :creator_id
end
end

User.rb:

class User < ApplicationRecord
has_many :winner_rooms,class_name:"Room",foreign_key:"winner_id"
has_many :creator_rooms,class_name:"Room",foreign_key:"creator_id"

end

Room.rb:

class Room < ApplicationRecord
belongs_to :winner,class_name:"User",foreign_key:"winner_id"
belongs_to :creator,class_name:"User",foreign_key:"creator_id"

end

Thanks to everyone who helped.

Rails: get belongs_to class_name

I think I've found what I'm looking for: ActiveRecord::Reflection class methods (the previous answers are helpful, but don't really answer the question)

I used the reflect_on_association class method to get the details of my :target association. So to get what I was looking for, I did:

UserEntry.reflect_on_association(:target).klass, which returned the User class.

belongs_to with a custom class_name not producing proper foreign key in Rails 3

You need to specify the foreign key on the has_many :products association, it does not automagically know that it mirrors the belongs_to :owner.

This should work:

class User < ActiveRecord::Base
has_many :products, :foreign_key => 'owner_id'
...
end

From the rails docs:

:foreign_key

Specify the foreign key
used for the association. By default
this is guessed to be the name of this
class in lower-case and "_id"
suffixed. So a Person class that makes
a has_many association will use
"person_id" as the default
:foreign_key..

Rails belongs_to returning nil

I am pretty sure your problem is, that the database field for the foreign key and the associated model are having the same name. The database field is made available as a member method called 'tourn' in the model by ActiveRecord and this obviously collides with your belongs_to definition which also creates a member method called 'tourn'. Try calling your model differently, at least in the association, using

 belongs_to :tournament, classname: 'Tourn', foreign_key: 'tourn'

Rails belongs_to association on model name that has underscores

You can set the class name on the association with class_name option.

class Account < ActiveRecord::Base
has_many :ui_1_logs, class_name: 'Ui_1_Log'
end

But I still recommend to follow conventions.

Programatically get the class of the belongs_to association in Rails 4

B.reflect_on_all_associations(:belongs_to).map(&:name)

or

b.class.reflect_on_all_associations(:belongs_to).map(&:name)

Rails belongs_to does not set foreign key id with custom class name

After reading through lots of Rails code and tracing here's what I found:

This bug exists because of the gem composite_primary_keys which overrode the default rails reflection.rb.

I will have to check how they implemented the primary_key_name and derive_primary_key_name methods.

It was quite a bit of time wasted on this silly bug, but at least I got to learn a lot about ActiveRecord's internals.



Related Topics



Leave a reply



Submit