I have the following models:
rails generate model RoomType code:string description:text
class RoomType < ActiveRecord::Base
has_many :rooms
end
rails generate model room name:string code:string
class Room < ActiveRecord::Base
belongs_to :room_type, foreign_key: "code"
end
I want to reference Room with RoomType on code and not room_type_id.
So I do #room.room_type.description in my rooms/show.html.erb and I get undefined method description for nil:NilClass
RoomType will only contain three codes i.e. AAA, BBB, CCC
Don't create a class called "Type". Change this name, maybe "Kind", I don't know.
Check this list Reserved words in rails
Changed it to best practice (it is not best practice to use foreign key as string):
rails g migration add_room_type_id_to_rooms room_type_id:integer
class RoomType < ActiveRecord::Base
has_many :rooms
end
class Room < ActiveRecord::Base
belongs_to :room_type
end
Related
I'm designing a simple Rails app with 2 models: Link and Category. Each link has exactly one category, and a category can have 0 or multiple links. What association should I apply to Link and Category? I'm very new to Ruby and those association terms are so confused to me.
You said each link has exactly one category
class Link < ApplicationRecord
belongs_to :category
end
category should exist before the link, so we go for belongs_to, not has_one
You said a category can have 0 or multiple links, so 0, 1, 2 or 79.
class Category < ApplicationRecord
has_many :links
end
I would suggest first to read active record associations
In your case
class Link
belongs_to :category
end
class Category
has_many :links
end
Don't get confused by the semantics when it gets to has_one and belongs_to.
The key difference is that belongs_to puts the foreign key on this models table:
class Link < ApplicationRecord
belongs_to :category
end
class Category < ApplicationRecord
has_many :links
end
You should read belongs_to :category as "this models table has a category_id column that references categories, it can only belong to a single category".
If you instead used:
class Link < ApplicationRecord
has_one :category
end
Rails would attempt to resolve the association through the categories.link_id column which won't work at all. has_one should be used when you have a one to one association on the the side without the foreign key:
class Country
has_one :capitol
end
class Capitol
belongs_to :country
end
I created a Model in a ruby on rails app rails g model subject_structure name:string abbreviation:string
I created the relations
class SubjectStructure < ActiveRecord::Base
has_many :subjects
end
class Subject< ActiveRecord::Base
belongs_to :subject_structure
end
When i run the app i get the error undefined method subject_structure for #<Subject:0x9d3aa78>
Im creating an app for an existing database that already has the tables with data in it.
Add a space after the belongs_to.
Should be
class Subject < ActiveRecord::Base
belongs_to :subject_structure
end
There's a space missing on the belongs_to:
class Subject< ActiveRecord::Base
belongs_to :subject_structure
end
Also, Subject must have a foreign_key for SubjectStructure. Run this migration to create it:
rails g migration AddSubjectStructureIdToSubject subject_structure_id:integer
Add a space like:
class Subject < ActiveRecord::Base
belongs_to :subject_structure
end
I have a question with Rails 3.1 associations. When you have a one-to-many association you put the has_many operators on the many side of the relationship and the usual example does something like this:
class Order < ActiveRecord::Base
belongs_to :customer
end
class Customer < ActiveRecord::Base
has_many :orders
end
My question is how should I do this if the class name is Orders? Should I put has_many :orders or should I always name my models using singular nouns?
Rails convention states that model names should always be singular, so you should never have a model class named Orders; it would probably just be Order.
I have two models one is named BusinessUser and the other is named BusinessPlace.
The BusinessUser can have many BusinessPlaces
class BusinessUser < ActiveRecord::Base
has_many :BusinessPlaces
end
class BusinessPlace < ActiveRecord::Base
belongs_to :BusinessUser
end
When i'm trying to access #business_user.BusinessPlaces.count the sql that get build and run on DB is
SELECT COUNT(*) FROM "business_places" WHERE "business_places"."business_user_id" = 1
but in the migration and in the database the column for business user id is BusinessUser_id which makes the query to fail. Why the sql gets to be build wrong ? I've used the console to create the models.
You just need to set the foreign_key the association will be using for :business_user:
class BusinessUser < ActiveRecord::Base
has_many :business_places
end
class BusinessPlace < ActiveRecord::Base
belongs_to :business_user, :foreign_key => 'BusinessUser_id'
end
You're using the wrong wording for the keys. Your models should look like so:
class BusinessUser < ActiveRecord::Base
has_many :business_places
end
class BusinessPlace < ActiveRecord::Base
belongs_to :business_user
end
so basically use :business_places instead of :BusinessPlaces
if you use migrations to set up your databases you should not need to modify the foreign keys
Please excuse the confusing phrasing in the title. In my RoR project let's say I have it set up like this
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
end
and
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
end
I then have a categories_products table that connects them. This works fine but my problem is that a product will only ever have one category at a time and I'd of course like to do product.category instead of having to deal with an array. How can I accomplish that?
A one-to-many representation is demonstrated in the rails guides like this:
class Category < ActiveRecord::Base
has_many :products
end
class Product < ActiveRecord::Base
belongs_to :category
end