How to seed a new joining table in a Rails API db - ruby-on-rails

I have two classes in my Rails API database, that I have just created a joining table for. I had seeded both classes previously so they have data. Now I would like to seed the joining class with id's from the existing data.
This is what the models look like for the first two classes:
class Stakeholder < ApplicationRecord
has_many :project_stakeholders
has_many :projects, through: :project_stakeholders
end
class Project < ApplicationRecord
has_many :project_stakeholders
has_many :stakeholders, through: :project_stakeholders
end
This is the model for my joining class:
class ProjectStakeholder < ApplicationRecord
belongs_to :project
belongs_to :stakeholder
end
Now I have two questions:
How to populate the joint class with data from the two first classes (project_id and stakeholder_id).
How can I check in the Rails console for values in the ProjectStakeholder class?
I have researched and tried different options from Stackoverflow but they don't seem to work. Rails guides do not provide an answer either, from what I have seen. Would be very grateful for some ideas!

You can just assign stakeholders to the project after creation
Project.first.stakeholders << Stakeholder.limit(5)
Project.second.stakeholders << Stakeholder.limit(5).offset(5)
You can check it in console in different ways:
Project.first.stakeholders.pluck(:id)
ProjectStakeholder.first

Related

Rails user profile devise :has_many or :has_many_through relationships

I'm wracking my brain about how to start setting up the following set of model relationships in my project. I'm not looking for the complete solution (models, tables, migrations, etc), just a little help to get me going in the right direction. I learn more by struggling on my own :)
In short, I want my Users (created with Devise) to have a set of Inclinations that represent preferences they have to certain things (i.e. warmer weather vs colder weather, watching sports vs playing sports, etc) on a sliding scale (coded 5 to -5, zero excluded). Each Inclination (or InclinationData?) will be in a separate table with a name, description, etc, and all Inclinations will get their data pulled from that table (so I don't repeat myself throughout my application). Also, every User will have one Inclination record for each InclinationData(?) by default.
As for the setup, will I need:
5 tables (3 main ones and two join tables)?
Just three (main three models)?
Four (main three models & a joining table for Inclinations and InclinationData)?
Four (main three models & a joining table for Users & Inclinations)?
Perhaps something simpler like a single Profile or Preferences model that's bound to a User?
I can never figure out how to decide if joining tables are necessary, and this is the most complex association I've ever tried.
I appreciate any help, and please let me know if you need more information to be able to help me out.
You say, all Users have the same set of Inclinations. You can put the description of the Inclinations in one model/table and the users ratings in the joining table UserInclination , according to eabraham:
class User < ActiveRecord::Base
has_many :users_inclinations
has_many :inclinations, through: :users_inclinations
end
class UsersInclination < ActiveRecord::Base
belongs_to :users
belongs_to :inclinations
attr_accessible :user_id, :inclination_id, preference_score
end
class Inclination < ActiveRecord::Base
has_many :users_inclinations
attr_accessible :description, :name, :low_label, :high_label
end
So you can pull the description (i.e. to build the rating form) from Inclinations and store the individual ratings with
an_inclination= Inclination.find_by_name('weather')
user.user_inclination.create(inclination: an_inclination, preference_score: the_users_score)
I think I've reasoned through your request:
Users has_many UserInclinations and Inclinations has_one UserInclinations. Roughly, the models should look like this:
class User < ActiveRecord::Base
has_many :user_inclinations
#default Devise attr_accessible
end
class UserInclinations < ActiveRecord::Base
belongs_to :users
belongs_to :inclinations
attr_accessible :user_id, :inclination_id, preference_score
end
class Inclination < ActiveRecord::Base
has_one :user_inclination
attr_accessible :preference
end

Rails Multiple HABTM

I'm building a rails application for an art exhibition website. At the moment I have 4 models, Curator, Exhibition, Artist, and Artwork.
The application should work as follow, An Exhibition can be curated by many curators, an exhibition can display multiple artworks, an artwork can be displayed in many exhibitions, artist can own many artworks, and artworks belongs to one artist.
I'm a rail newb and I'm having difficulty building the relationship between the models. Can you tell me if I'm doing this right, or maybe there is a better way?
curator.rb
class Curator < ActiveRecord::Base
has_and_belongs_to_many :exhibitions
end
exhibition.rb
class Exhibition < ActiveRecord::Base
has_and_belongs_to_many :curators
has_and_belongs_to_many :artworks
end
artwork.rb
class Artwork < ActiveRecord::Base
has_and_belongs_to_many :exhibitions
belongs_to :artist
end
artist.rb
class Artist < ActiveRecord::Base
has_many :artworks
end
Thanks!
I would recommend using has_many :through if you want to have validations on your relationships, use callbacks or want to add extra attributes.
If you want to have more information regarding this matter, this rails guide is great:
http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many
Your HABTM looks fine though. If you run into any problem, it would be advisable to also reference your joined tables.
It looks right to me.. Do create the join tables naming convention would be:
curators_exhibitions
artworks_exhibitions
Hope you get the idea.
Also you would want to check out has_many :through in case you would want to do validations on top of the relationships!

Rails 2 tables, 1 model

I am relatively new to ruby/rails and I have the following question:
I am working on a scheduling app and have a model named Classes and another named ClassEntries. The relationship between them is that each user can have multiple class entries per semester, each relating to one class. Each record in the Classes table belongs to a specific University. A User can have multiple entries in the ClassEntries table for 1 semester (typically 5). Their schedule is comprised of all their ClassEntries with the same semester ID.
I am not sure whether I should have a third model called Schedule that brings together the info in the ClassEntries and Classes models for the user at hand. I originally wrote this functionality in PHP and I simply used a MySQL JOIN to gather the necessary information. In Rails it seems that there should be a better way to accomplish this.
What would be the best way of going about this in Rails?
Many thanks
So, what you are looking for is pretty much associations in Rails.
You would have the following:
def User < ActiveRecord::Base
has_many :course_entries
has_many :courses, :through => :class_entries
end
def CourseEntry < ActiveRecord::Base
belongs_to :user
belongs_to :course
end
def Course < ActiveRecord::Base
has_many :course_entries
has_many :users, :through => :class_entries
end
With those associations set up, Rails would allow you to do such things like
some_user.courses or some_course.users and it will make the joins through CourseEntry for you.
Let me know if this helps. If you need me to go more in depth let me know.

rails model relationship and migration

I have some problem trying to understand when building a rails app with several models and relation ships between them...
If I take a basic example like a model Group, a model User and a model Car
class Group < ActiveRecord::Base
has_many :users
end
class User < ActiveRecord::Base
belongs_to :group
has_many :cars
end
class Car < ActiveRecord::Base
belongs_to :user
end
Will those relation ship statements automatically create the following functions:
group.users
user.group
user.cars
car.user
It seems that we sometimes need to have to create "references" in migration (like adding a reference toward User in Car table) but is this always required ?
In this case, what is the difference of creating the migration and of adding the relationship statement in the models ? I sometimes have the feeling this is used for the same purpose.
Thanks a lot for your help,
Regards,
Luc
The association declarations are there for Rails only. You have to define the foreign keys (references) in the database, so that Rails can properly save the data.
Remember, despite all the magic, it's still backed by a relational database, so good practices there will pay off in the long run.

What is the relationship between these two tables in RoR?

I am developing an application like the stackoverflow, which questions or articles have at less one tag. And one tags must have one or more articles.
So, I am doing this in migration in RoR. I am consider which relationship is suitable for both table. In article table, should use a "has_many", and in the tag table, should use "has_many".
But I am thinking is it necessary to add one more table in the middle, something like....
So, the first one is like that:
class Article < ActiveRecord::Base
has_many :tags
end
class Tag < ActiveRecord::Base
has_many :articles
end
or something like this:
class Article < ActiveRecord::Base
has_many :articleTagList
has_many :tags, :through => : articleTagLists
end
class Tag < ActiveRecord::Base
has_many :articleTagList
has_many :articles, :through => :articleTagLists
end
class ArticleTagList < ActiveRecord::Base
belongs_to :article
belongs_to :tag
end
Many-to-Many relationships in a normalized database will always need a third "look-up table."
If you denormalize you can get away with just having the tag id's in one field with a delimiter between them. But you also have to provide the logic to handle retrieval.
I'd personally just go with the normalized option.
If you don't want to store any information on the middle table (for example the name of the user who added tag X to the question Y), you can use the has_and_belongs_to_many:
http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_and_belongs_to_many
If you want to store something, you need to create the middle model, as your example. In your example, the ArticleTagList model should be called ArticlesTag and the database table should be articles_tags, by convention.

Resources