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
Related
I'm having difficulty with bulding attributes within my model. I keep seeing this error
NoMethodError: undefined method `users' for #<ActiveRecord::ConnectionAdapters::SQLite3::TableDefinition:0x00007f9d9ad9df98>
I need attributes that look like this:
#group.rb
class Spree::Group < ActiveRecord::Base
has_many :users
end
#user_decorator.rb
Spree::User.class_eval do
belongs_to :group, class_name: "Spree::Group"
end
For group.rb I ran
rails g model Spree::Group
and I got:
class Spree::Group < ApplicationRecord
has_many :users
end
I'm confused on how to add 'has_many :users' without actually going into the
model and inputting it there.
For the user_decorator I'm not sure what that migration would look like. Any help would be fantastic!
You could call:
rails g model group user:references
which will generates an user_id column in the groups table and will modify the group.rb model to add a belongs_to :user relatonship. Please note, you must to put manually the has_many :groups or has_one :group relationship to the user.rb model.
If you already have the model generated, you could create a migration with the following:
rails g migration AddUserToGroup user:belongs_to
which will generate:
class AddUserToGroup < ActiveRecord::Migration
def change
add_reference :groups, :user, index: true
end
end
the only difference with this approach is, the belongs_to :user relationship in the group.rb model won't be created automatically, so you must create it for your own.
I have two models:
User
class User < ActiveRecord::Base
has_and_belongs_to_many :partners
end
and Partner
class Partner < ActiveRecord::Base
has_and_belongs_to_many :users
end
and now, i want change it to:
class User < ActiveRecord::Base
has_many :partners
end
class Partner < ActiveRecord::Base
belongs_to :user
end
but how can i do it by migration?
You can change the model's to:
class User < ActiveRecord::Base
has_many :partners
end
class Partner < ActiveRecord::Base
belongs_to :user
end
And Partner model should have a user_id column
If you don't have a user_id column in Partner model, you can add it by:
rails g migration add_user_id_to_partner user_id:integer
The intermediate table partners_users should be dropped since it is a has_many Association.
To drop the table create an empty migration and then add this to the migration file.
drop_table :partners_users
Then run the migration using rake db:migrate
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
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
I have 2 models in different namespace.
class Admin::Membership < ActiveRecord::Base
has_many :authorization_roles
end
class AuthorizationRole < ActiveRecord::Base
belongs_to :membership
end
The Membership model is in different folder with AuthorizationRole model (I don't know what is called)
When run Admin::Membership.find(:all), the data from AuthorizationRole model is not included. I've create membership_id field on authorization_roles table, but I still can't get both models related. Is something wrong in this code? Sorry if I'm missing something basic here.
Try this
class Admin::Membership < ActiveRecord::Base
has_many :authorization_roles, :class_name => '::AuthorizationRole'
end
class AuthorizationRole < ActiveRecord::Base
belongs_to :membership, :class_name => 'Admin::Membership'
end
I've never used namespaced models and I don't think you need to... but maybe you'll have to specify the class name in AuthorizationRole, something like:
belongs_to :membership, :class_name => 'Admin::Membership'
UPDATE:
Assuming you have:
class Membership < ActiveRecord::Base
has_many :authorization_roles
end
class AuthorizationRole < ActiveRecord::Base
belongs_to :membership
end
You have added an integer column called membership_id to authorization_roles and you've run the migrations. Now you should be able to create authorization_roles like this #membership.authorization_roles.create( ... ) and fetch them #membership.authorization_roles
Check to see if you are setting the table name prefix. The Rails model generator adds a file like this for namespaced models:
# /app/models/admin.rb
module Admin
def self.table_name_prefix
'admin_'
end
end
Note: this is Rails version 3.0.1 -- not sure about earlier versions.