schema.rb:
ActiveRecord::Schema.define(version: 20150324012404) do
create_table "groups", force: :cascade do |t|
t.string "title"
t.integer "teacher_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "groups_students", id: false, force: :cascade do |t|
t.integer "group_id"
t.integer "student_id"
end
add_index "groups_students", ["group_id"], name: "index_groups_students_on_group_id"
add_index "groups_students", ["student_id"], name: "index_groups_students_on_student_id"
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "admin", default: false
t.string "type"
t.integer "group_id"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
group.rb:
class Group < ActiveRecord::Base
belongs_to :teacher
has_and_belongs_to_many :students
end
student.rb:
class Student < User
has_and_belongs_to_many :groups
end
I could have set a simple belongs_to and a has_many relationship between the student and group models, but I want students to be able to belong to more than one group, so I set up a HABTM association and corresponding join table.
I think I that right?
The question is, how do I, in the console, set a Student to belong to more than one group?
I have setup a User with 'type: Student' and I have two Groups. So...
In the console I do:
student = Student.first
Then, I want to set 'student' to belong to both Groups, but I don't know how to do this.
To set it to belong to one group I can do:
student.update_attributes(group_id: 1)
But how do make it belong to both groups? It would have two group_id's wouldn't it? I don't know how to set this.
If you need to see any of the other files, it's the 'handcode' branch here:
https://github.com/Yorkshireman/sebcoles/tree/handcode
The answers others have already provided are correct. But if you're working with id's you can also do something like this
student = Student.first
student.group_ids = 1,2,3,4
You don't need to set group_id for the User, the association is handled by the join table and the HABTM statement. You should remove group_id from the users table in the schema.
From memory you should be able to do something like this:
student = Student.first
groups = Group.all
student.groups << groups
student.save
See the active record guide on HABTM associations - specfically 4.4.1.3
Instead of habtm, just use the normal through and your life becomes easy. Make sure an id is generated for the association table (remove id:false)
create_table "group_students", force: :cascade do |t|
t.integer :group_id, nil:false
t.integer :student_id, nil:false
end
class Group < ActiveRecord::Base
has_many :group_students, dependent: :destroy, inverse_of :group
has_many :students, through :group_students
end
class Student < ActiveRecord::Base
has_many :group_students, dependent: :destroy, inverse_of :student
has_many :groups, through: :group_students
end
class GroupStudent < ActiveRecord::Base
belongs_to :group,
belongs_to :student
validates_presence_of :group, :student
end
Group.last.students << Student.last
or..
Student.last.groups << Group.last
Student.last.groups = [Group.find(1), Group.find(2)]
etc....
Ok, so it took me 3 days of all kinds of pain to work this out.
There was nothing wrong with my original code, except that I needed to remove the group_id from the user table.
roo's answer was correct, except that using 'group' as a variable name in the console confused Rails. This had led me to believe there was something wrong with my code, but there wasn't. You learn the hard way.
So, Students can be pushed into Groups like this:
To push a student into one group:
student = Student.first
OR
student = Student.find(1)
(or whatever number the id is)
group1 = Group.first
OR
group1 = Group.find(1)
student.groups << group1
To push into multiple groups (which was the original goal of this whole debacle:
student = Student.first
OR
student = Student.find(1)
allclasses = Group.all
student.groups << allclasses
To view your handywork:
student.groups
Works beautifully. The only problem I can see with my code is that it's possible to push the same student into a group twice, resulting in two duplicates of that student in one group. If anyone knows how to prevent this happening, I'm all ears.
Related
I'm currently working on a small school project that utilizes Ruby on Rails and I'm having some trouble getting my self-referential associations working correctly.
Context
The intended functionality of my web app is for users to post houses/apartments for other users to search through and rent. Since I'm having issues with a specific association, I'm working with a completely stripped down version that only has two models, User and Lease.
What I'm Trying to Accomplish
Ideally, when a person first registers on the site, a User object is created to hold their information such as email and password. A User can then either post a listing or search through listings.
Once a post has been created and another user decides to rent the posted house, a Lease object is created, which holds the ID of the posting User as well as the ID of the renting user, aliased as "landlord_id" and "tenant_id" respectively.
A User should now be identified as either a User, Landlord or a Tenant (or both Landlord and Tenant) based on whether there are any Lease objects with their ID as either a Landlord or a Tenant. This identification will be used to determine whether the User can access other areas of the site.
userFoo.leases
This should give me a list of all Lease objects with which the User's ID is associated, regardless of whether it's as a Landlord or Tenant.
userFoo.tenants
This should give me a list of any User object whose ID is associated with the ID of userFoo as a Tenant through Lease, and the inverse if I ask for landlords.
The Code
User Class
class User < ApplicationRecord
has_many :tenants, class_name: "Lease", foreign_key: "landlord_id"
has_many :landlords, class_name: "Lease", foreign_key: "tenant_id"
end
Lease Class
class Lease < ApplicationRecord
belongs_to :landlord, class_name: "User"
belongs_to :tenant, class_name: "User"
end
Users Table Migration
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :password_digest
t.timestamps
end
end
end
Leases Table Migration
class CreateLeases < ActiveRecord::Migration[6.0]
def change
create_table :leases do |t|
t.references :landlord, null: false, foreign_key: {to_table: :users}
t.references :tenant, null: false, foreign_key: {to_table: :users}
t.timestamps
end
end
end
Database Schema
ActiveRecord::Schema.define(version: 2020_10_18_005954) do
create_table "leases", force: :cascade do |t|
t.integer "landlord_id", null: false
t.integer "tenant_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["landlord_id"], name: "index_leases_on_landlord_id"
t.index ["tenant_id"], name: "index_leases_on_tenant_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.string "password_digest"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
add_foreign_key "leases", "users", column: "landlord_id"
add_foreign_key "leases", "users", column: "tenant_id"
end
What's Wrong?
userFoo.leases
Normally a User would have_many leases by having their ID associated with a lease as "user_id." However, since I'm using "tenant_id" and "landlord_id", this command fails because it can't find "user_id" in the Leases table.
userFoo.tenants
This command gives me a list of all Lease objects where userFoo's ID is associated as "landlord_id" instead of all User objects associated with userFoo's ID as tenants. To retrieve a tenant as is, I have to use the command:
userFoo.tenants.first.tenant
Conclusion
I am having a bit of a hard time understanding these deeper, more complex associations, and I've spent some time trying to find a detailed reference on has_many that covers all the arguments, but all I can really find are small blog posts that reference the "Employees" and "Managers" example on guides.rubyonrails.com . I think one problem is that I'm not sure I'm correctly reflecting my model associations in my table schema.
I'm more than happy to teach myself if someone can point me in the right direction. I'm also open to alternative solutions but only if I can't get the functionality I want out of this setup, because my instructor specifically asked me to try it this way
Thanks in advance for any help! It's much appreciated.
as per your requirement you can try like this:
# app/models/user.rb
class User < ApplicationRecord
has_many :owned_properties, class_name: "Property", foreign_key: "landlord_id"
has_many :rented_properties, class_name: "Property", foreign_key: "tenant_id"
end
Here I have declared two associations with same table but different foreign keys.
# app/models/property.rb
class Property < ApplicationRecord
belongs_to :landlord, class_name: "User"
belongs_to :tenant, class_name: "User"
end
Here I have taken one table by using this user can post one property where landlord is the owner of a house and later you can add tenant who is taking rent to one property.
# db/migrations/20201018054951_create_users.rb
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :name, null: false
t.string :email, null: false, index: true
t.string :password_digest, null: false
t.timestamps
end
end
end
Above is your users table migration.
# db/migrations/20201018055351_create_properties.rb
class CreateProperties < ActiveRecord::Migration[6.0]
def change
create_table :properties do |t|
t.references :landlord, foreign_key: {to_table: :users}, null: false
t.references :tenant, foreign_key: {to_table: :users}
t.timestamps
end
end
end
Above is your properties table migration.
# db/schema.rb
ActiveRecord::Schema.define(version: 2020_10_18_055351) do
create_table "properties", force: :cascade do |t|
t.bigint "landlord_id", null: false
t.bigint "tenant_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["landlord_id"], name: "index_properties_on_landlord_id"
t.index ["tenant_id"], name: "index_properties_on_tenant_id"
end
create_table "users", force: :cascade do |t|
t.string "name", null: false
t.string "email", null: false
t.string "password_digest", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["email"], name: "index_users_on_email"
end
add_foreign_key "properties", "users", column: "landlord_id"
add_foreign_key "properties", "users", column: "tenant_id"
end
If you want to fetch all the owned properties of a user, use user.owned_properties.
If you want to fetch all rented properties of a user, use user.rented_properties.
^^ Here both the cases you'll get objects of Property class.
If you want to get landlord of a property, use property.landlord.
If you want to get tenant of a property, use property.tenant.
^^ Here both the cases you'll get objects of User class.
If you want you can add other attributes like: name, price, etc to properties table.
I think, this will help you. Thanks :) Happy Coding :)
Issue is I can't find why reference column id can't be inserted when create new record.
I have 3 table shop_plan, shop and app
Below is tables schema:
create_table "shop_plans", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "shops", force: :cascade do |t|
t.string "url"
t.bigint "plan_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["plan_id"], name: "index_shops_on_plan_id"
end
create_table "apps", force: :cascade do |t|
t.bigint "shop_id"
t.binint "amount"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["app_id"], name: "index_apps_on_shop_id"
end
add_foreign_key "shops", "shop_plans", column: "plan_id"
add_foreign_key "apps", "shops"
And below is Model
class ShopPlan < ApplicationRecord
has_many :shop
end
class Shop < ApplicationRecord
belongs_to :shop_plan, class_name: 'ShopPlan', foreign_key: :plan_id
has_many :app
end
class App < ApplicationRecord
belongs_to :shop, class_name: 'Shop', foreign_key: :shop_id
end
There will be 1 default record added in seed.db for table shop_plan
ShopPlan.create(name: 'Basic')
ShopPlan and Shop are linked by plan_id column in Shop
Shop and App are linked by shop_id column in App
I pre-insert some value when user access index:
#basic_plan
#basicPlan = ShopPlan.where(name: "Basic").first
# if new shop registered, add to database
unless Shop.where(url: #shop_session.url).any?
shop = Shop.new
shop.url = #shop_session.url
shop.plan_id = #basicPlan.id
shop.save
end
This insert works well, however, when i run 2nd insert:
#shop= Shop.where(url: #shop_session.url).first
unless App.where(shop_id: #shop.id).any?
app = App.new
app.shop_id = #shop.id,
app.amount = 10
app.save
end
error occurs as somehow app.shop_id will not add in my #shop.id and it will return will error: {"shop":["must exist"]}
I even try hard-code app.shop_id =1 but it does not help and when I add in optional: true to app.db model, it will insert null
Appreciate if anyone can help point out why I get this error
EDIT: #arieljuod to be clear
1) I have to specific exact column class due to between Shop And Shop_Plan, i'm using a manual plan_id instead of using default shopplans_id columns.
2) I have update 1 column inside App and all that unless is just to do checking when debugging.
First of all, like #David pointed out, your associations names are not right. You have to set has_many :shops and has_many :apps so activerecord knows how to find the correct classes.
Second, you don't have to specify the class_name option if the class can be infered from the association name, so it can be belongs_to :shop and belongs_to :shop_plan, foreign_key: :plan_id. It works just fine with your setup, it's just a suggestion to remove unnecesary code.
Now, for your relationships, I think you shouldn't do those first any? new block manually, rails can handle those for you.
you could do something like
#basicPlan = ShopPlan.find_by(name: "Basic")
#this gives you the first record or creates a new one
#shop = #basicPlan.shops.where(url: #shop_session.url).first_or_create
#this will return the "app" of the shop if it already exists, and, if nil, it will create a new one
#app = #shop.app or #shop.create_app
I have found out the silly reason why my code does not work.
It's not because as_many :shops and has_many :app and also not because my code when creating the record.
It just due to silly comma ',' when creating new record in App at app.shop_id = #shop.id,, as I was keep switching between Ruby and JavaScript. Thank you #arieljuod and #David for your effort
I have two models: University and Market
A University belongs to a single Market, and a Market can have many universities. For example, An instance of Market like Boston might have University instances like MIT, Harvard, Boston University, etc.
I want to be able to do something like this in console:
University.first.market.name
But I get the following error:
NoMethodError: undefined method `market' for #
I can get the market id from University.first.market_id, but I can't get the name from market.name.
Here is how I have my models set up:
class University < ApplicationRecord
belongs_to :markets
class Market < ApplicationRecord
has_many :universities
end
And here is my schema - I think with the market_id integer column and index correctly implemented (?)
create_table "universities", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "full_name"
t.integer "market_id"
t.index ["market_id"], name: "index_universities_on_market_id"
end
Here's my markets schema:
create_table "markets", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
end
What am I doing incorrectly?
In a one-to-many relationship, the "belongs_to" end must be singular, as below:
class University < ApplicationRecord
belongs_to :market
end
class Market < ApplicationRecord
has_many :universities
end
Let me know if that helps
EXPLANATION
In my Rails 5 application I have,
Parent model order.rb
class Order < ApplicationRecord
has_many :tasks
end
Child model task.rb
class Task < ApplicationRecord
belongs_to :order
belongs_to :rider
end
rider.rb
class Rider < ApplicationRecord
has_many :tasks
end
Parent and Task attributes schema.rb
create_table "orders", force: :cascade do |t|
t.string "status"
t.boolean "urgent"
t.date "schedule"
t.integer "customer_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "company_id"
t.index ["company_id"], name: "index_orders_on_company_id", using: :btree
t.index ["customer_id"], name: "index_orders_on_customer_id", using: :btree
end
create_table "tasks", force: :cascade do |t|
t.boolean "task_type"
t.string "details"
t.string "address"
t.string "nearby"
t.float "lat"
t.float "lng"
t.string "name"
t.string "contact"
t.time "time"
t.string "item_type"
t.integer "order_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "rider_id"
t.integer "status"
t.index ["order_id"], name: "index_tasks_on_order_id", using: :btree
end
Now let's assume,
I have 3 orders in my database, each order has 5 tasks.
1 out of 5 tasks of first 2 orders belongs to this rider my_rider = Rider.find(1)
So basically, no matter how many tasks are there in an order, if a rider has any of the task assigned, when we show #assigned_orders on my_rider dashboard there should be all the orders in which my_rider has a task assigned, which in my case is the first 2 out of 3 orders.
I don't know if I explained it right :/
PROBLEM
I want to show the #assigned_orders of my_rider on his dashboard.
I want my ActiveRecord to make a single query and get all orders along with the their tasks.
I tried this,
my_rider = Rider.find(1)
#assigned_orders = Task.joins(:order).where(rider: my_rider)
But it gives me the tasks objects in #assigned_orders instance variable not the orders.
I cannot query with the Parent (Order) model because it has no association with the Rider model.
Please help me find a solution for this problem. Thanks in advance!
It is as simple as ABC. Add has_many :through association, to set up a connection between riders and orders.
class Rider < ApplicationRecord
has_many :tasks
has_many :orders, -> { distinct }, through: :tasks
end
And then use it.
my_rider = Rider.find(1)
#assigned_orders = my_rider.orders
You can also do it without the association.
#assigned_orders = Order.joins(:tasks).where(tasks: {rider_id: my_rider.id}).distinct
You have rider_id column in your tasks table. Therefore I guess you can make the following query:
Order.joins(:tasks).where(tasks: { rider_id: my_rider.id }).distinct
I am trying to create a many to many relationship between my groups table and my keywords table.
When I am in my controller I cant do Keyword.groups or Group.keywords as I get a no method error. I have checked Group.methods and I only have these related methods
"before_add_for_groups_keywords",
"before_add_for_groups_keywords?",
"before_add_for_groups_keywords=",
"after_add_for_groups_keywords",
"after_add_for_groups_keywords?",
"after_add_for_groups_keywords=",
"before_remove_for_groups_keywords",
"before_remove_for_groups_keywords?",
"before_remove_for_groups_keywords=",
"after_remove_for_groups_keywords",
"after_remove_for_groups_keywords?",
"after_remove_for_groups_keywords=",
"before_add_for_keywords",
"before_add_for_keywords?",
"before_add_for_keywords=",
"after_add_for_keywords",
"after_add_for_keywords?",
"after_add_for_keywords=",
"before_remove_for_keywords",
"before_remove_for_keywords?",
"before_remove_for_keywords=",
"after_remove_for_keywords",
"after_remove_for_keywords?",
"after_remove_for_keywords=",
Where as Keyword.methods gives me these
"before_add_for_keywords_groups",
"before_add_for_keywords_groups?",
"before_add_for_keywords_groups=",
"after_add_for_keywords_groups",
"after_add_for_keywords_groups?",
"after_add_for_keywords_groups=",
"before_remove_for_keywords_groups",
"before_remove_for_keywords_groups?",
"before_remove_for_keywords_groups=",
"after_remove_for_keywords_groups",
"after_remove_for_keywords_groups?",
"after_remove_for_keywords_groups=",
"before_add_for_groups",
"before_add_for_groups?",
"before_add_for_groups=",
"after_add_for_groups",
"after_add_for_groups?",
"after_add_for_groups=",
"before_remove_for_groups",
"before_remove_for_groups?",
"before_remove_for_groups=",
"after_remove_for_groups",
"after_remove_for_groups?",
"after_remove_for_groups=",
My models
has_and_belongs_to_many :keywords
has_and_belongs_to_many :groups
My db schema is the following
create_table "groups", force: :cascade do |t|
t.integer "member_id"
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "groups", ["member_id"], name: "index_groups_on_member_id", using: :btree
create_table "groups_keywords", id: false, force: :cascade do |t|
t.integer "group_id"
t.integer "keyword_id"
end
add_index "groups_keywords", ["group_id", "keyword_id"], name: "index_groups_keywords_on_group_id_and_keyword_id", unique: true, using: :btree
create_table "keywords", force: :cascade do |t|
t.string "keyword"
t.string "keyword_hash"
t.datetime "checked_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Associations are defined for instances.
Keyword.groups indeed does not exist (as a class method).
However, Keyword#groups, as in Keyword.first.groups, does work. The reason for that is that Keyword.first returns an instance of Keyword class, which Keyword class itself is not.
A better way to implement a many to many relationship is with the has_many :through idiom:
class Keyword
has_many :keyword_groups
has_many :groups, :through => :keyword_groups
class Group
has_many :keyword_groups
has_many :keywords, :through => :keyword_groups
class KeywordGroup
belongs_to :keyword
belongs_to :group
This will give you flexibility in the future if you need to extend your join model.
When you do Keyword.group(1), it doesn't do what you think it is doing. It just finds all the keywords and groups them by 1.
SELECT * "keywords".* FROM "keywords" GROUP BY 1
Indeed, you can't actually call these kind of method on Class level, associations are accessed through instance level. And as you have defined them has_and_belongs_to_many, you will have to be plural about them, not single.
keyword = Keyword.first
keyword.groups # Not 'group', and on instance level