Rails 2.3.8 has_many :through - mysql

I have 3 Tables/Classes
vectors, b_cells and transfections
They are associated like this
class BCell
has_and_belongs_to_many :vectors ( JOIN )
class Transfection
has_and_belongs_to_many :vectors ( JOIN )
How do I connect b_cells and transfections using the associations?
I tried
class BCell
has_many :transfections, :through => :vectors
class Transfection
has_many :b_cells, :through => :vectors
I am using rails 2.3.8

I think you are having a problem with the model declaration. Can you try this and see if it will work
class BCell < ActiveRecord::Base
has_many :vectors
has_many :transfections, :through => :vectors
end
class Transfection < ActiveRecord::Base
has_many :vectors
has_many :b_cells, :through => :vectors
end
class vectors < ActiveRecord::Base
belongs_to :b_cell
belongs_to :transfection
end

Related

Struggling to add has_many through relationship

I am trying to make a has_many through relationship like this:
#user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :availabilities
has_many :timeslots, :through => availabilities
end
#availability.rb
class Availability < ApplicationRecord
belongs_to :timeslot
belongs_to :user
end
#timeslot.rb
class Timeslot < ApplicationRecord
has_many :availabilities
has_many :timeslots, :through => availabilities
end
I created the two models and than ran rake db:migrate without adding the code in the models (to create the tables).
I made a migration file:
class AddFieldsToTables < ActiveRecord::Migration[5.0]
def change
add_column :users, :availability_id, :integer
add_column :timeslots, :availability_id, :integer
add_column :availabilities, :user_id, :integer
add_column :availabilities, :timeslot_id, :integer
end
end
and ran rake db:migrate
Than I added the code above to all the files.
And then if I try to generate anything it gives me NameError: undefined local variable or method availabilities for User (call 'User.connection' to establish a connection):Class
I am new to Ruby on Rails.
One issue I see is that in your timeslot.rb you have has_many :timeslots, :through => availabilities. I'm guessing you want has_many :users, :through => :availabilites.
Another is in user.rb, you have has_many :timeslots, :through => availabilities but you need the symbol :availabilites. This is what is causing the error you posted, I believe. It should look like this (all I've changed is the second-to-last line):
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :availabilities
has_many :timeslots, :through => :availabilities
end
I see a tiny problem in your code:
#timeslot.rb
class Timeslot < ApplicationRecord
has_many :availabilities
has_many :timeslots, :through => availabilities
end
it should be:
#timeslot.rb
class Timeslot < ApplicationRecord
has_many :availabilities
has_many :users, :through => availabilities
end
I'm not sure if it can solve your problem but your code (exclude the above mistake) sounds fine for me.
In order to setup has_many through relationship between two tables users and timeslots, you need to setup join table availabilities with columns user_id and timeslot_id.
Setup your rails models like below:
# models/user.rb
class User < ApplicationRecord
has_many :availabilities
has_many :timeslots, :through => :availabilities
end
# models/availability.rb
# projects table should have these columns - user_id:integer, timeslot_id:integer
class Availability < ApplicationRecord
belongs_to :timeslot
belongs_to :user
end
# models/timeslot.rb
class Timeslot < ApplicationRecord
has_many :availabilities
has_many :users, :through => :availabilities
end
You need a migration to create availabilities table which acts as a join table for your has_many through relationship between Timeslot Object and User Object. Migration file looks something like this:
class CreateAvailabilities < ActiveRecord::Migration[5.0]
def change
create_table :availabilities do |t|
t.integer :user_id
t.integer :timeslot_id
end
end
end
Access
User.last.timeslots gives 0, 1 or many timeslots associated with User.last
Timeslot.last.users gives 0, 1 or many users associated with Timeslot.last

rails_admin has_many through with extra field

I have 2 models. User and Project. And there is a many_to_many relation with and extra position field between them. I can't see that extra field on rails_admin edit page.
How can add that field to form?
user.rb
has_many :projects, :through => :works_ons
project.rb
has_many :users, :through => :works_ons
works_on.rb
attr_accessible :position, :project_id, :user_id
belongs_to :user
belongs_to :project
Is it true that your user model has_many :users, :through => :works_ons?
I'm wondering if you need
user.rb
has_many :projects, :through => :works_ons
This is the closest I found. You should probably add a custom field below the association and enter the extra fields.
https://github.com/sferik/rails_admin/wiki/Has-many-%3Athrough-association

Associate foreign key through another foreign key

I've these 3 Models
class User < ActiveRecord::Base
has_many :answers, :as => :owner
end
class Answer < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
has_one :test
end
class Test < ActiveRecord::Base
belongs_to :answer
end
so I want to associate the Test model with User model through Answer Model without need of creating new association between them, so I put the following into Test Model:
has_one :owner, :through => :answer
but it doesn't work and I got this error
ActiveRecord::HasManyThroughAssociationPolymorphicSourceError: Cannot have a has_many :through association 'Test#owner' on the polymorphic object 'Owner#owner'.
any help?
In Test:
delegate :owner, :to => :answer
You have to specify source_type option as owner is a polymorphic association
class Test < ActiveRecord::Base
belongs_to :answer
has_one :owner, :through => :answer, :source_type => "User"
end

Direct association for a Group with a User

I have Group model that has_many institutions, and schools.
class Group
has_many :institutions
has_many :schools
end
However I need my Group to have_and_belongs_to_many :users but I want all the users that are associated through the institutions and schools like this:
class Group
has_many :institutions
has_many :users, :through => :instiutions
has_many :schools
has_many :users, :through => :schools
end
class School
belongs_to :group
has_many :educations
has_many :users, :through => :educations
end
class Institution
belongs_to :group
has_many :institutional_educations
has_many :users, :through => :institutional_educations
end
Obviously this isn't possible so what should I do?
Just offhand, have you considered using single table inheritance, so that school is a type of institution?
Your School class would inherit from Institution ("class School < Institution"). Plus, I guess you'd call it something else but you could have "class GenericInstitution < Institution" as well. Then your Group class could look like this:
class Group
:has_many :school_users, :through => :schools,
:has_many :generic_institution_users, :through => :generic_institutions
# if you need all of the users at once:
:has_many :users, :through => :institutions
end
You'd probably have to specify a foreign key or two to get this to work for you.
Also, I can't quite figure see what an :institutional_education is, but if you really need it you could do the same thing there (maybe "class institutional_education < education", or maybe the other way around.

Rails ActiveRecord Multiple Levels of Association

I'm having a little trouble with querying multiple tables with different types of association. Can someone please point me in the right direction?
class Sale < ActiveRecord::Base
has_many :items, :dependent => :destroy
end
class Item < ActiveRecord::Base
belongs_to :sale, :dependent => :destroy
has_many :images, :dependent => :destroy
end
class Image < ActiveRecord::Base
belongs_to :item, :dependent => :destroy
end
What would be the query to get all items relating to a sale with the ID 1, and then loop through all the images relating to each item returned?
Thanks for your help.
You can define :through => :something in a has_many association
class Sale < ActiveRecord::Base
has_many :items, :dependent => :destroy
has_many :images, :through => :items
end
and then simply query
Sale.find(1).images