Rails - has_many relation causes NoMethodError - ruby-on-rails

I've the following migrations and models:
class CreatePlatforms < ActiveRecord::Migration
def change
create_table :platforms do |t|
t.integer :user_id
t.string :name
t.string :platform_id
t.timestamps
end
end
end
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.string :first_name
t.string :last_name
t.string :gender
t.date :birthday
t.timestamps
end
end
end
class Platform < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
attr_accessible :email, :first_name, :last_name, :gender, :birthday
has_many :platforms
end
With this definition I can create users and platforms:
user = User.new
platform1 = Platform.new
platform2 = Platform.new
And even I can associate users to platforms:
platform1.user = user
But when I try to associate platforms to users or get the platforms from a users it crashes:
user.platforms << user or user.platforms
NoMethodError: undefined method `platforms' for #<User:0x007f8cfd47a770>

As it turns out, the problem is the database field platform_id. This messes up rails. Simply delete it and it'll work.

Related

Rails belongs to association method undefined

Hi i'm having a problem with a rails association. I have the Table Users and the table Roles. Here are my migrations:
class CreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
t.string :email
t.string :password_digest
t.belongs_to :role, index: true, foreign_key: true
t.timestamps
end
end
end
class CreateRoles < ActiveRecord::Migration[5.2]
def change
create_table :roles do |t|
t.string :name
t.string :code
t.timestamps
end
end
end
Im having the problem when I create a user with a role that I had previously created
Role.create(name: 'Super Admin', code: 'super_admin')
User.create(email: 'a#b.com', password: 'abcdefg', role_id: 1)
When I try to do User.first.role I get that the method role is undefined. As far as i know When i do that i should get an active record with the role.
What i am doing wrong. Please Help
You need to add the relation to your model. In user.rb:
class User < ApplicationRecord
belongs_to :role
# other code
end
It will generate the ActiveRecord methods you are looking for.

How do I set/limit the input in a rails form to match another models table?

I'm quite new to programming and haven't been able to find any resources to help me with this. I have created two scaffolds Accounts and Cashbooks. I want users to be able to add an account (with a parameter of account_name) which will then be set as the parameters for the Cashbook account input. Ideally when adding a Cashbook transaction, I would want users to see a dropdown which will contain all the account_names of the Accounts which have been created.
Cashbook Model
class Cashbook < ActiveRecord::Base
belongs_to :account, foreign_key: :account_name
end
Cashbook Controller
def new
#cashbook = Cashbook.new
#cashbook.account(account_params[:account_name])
end
def create
#cashbook = Cashbook.new(cashbook_params)
#cashbook.account(account_params[:account_name])
def cashbook_params
params.require(:cashbook).permit(:date, :description, :account, :kind, :amount, :balance, :name)
params.require(:account).permit(:account_name)
end
Cashbook DB
class CreateAccounts < ActiveRecord::Migration
def change
create_table :accounts do |t|
t.string :account_name
t.boolean :operating_expense
t.boolean :cost_of_sales
t.boolean :sales
t.boolean :other_income
t.boolean :non_current_liability
t.boolean :non_current_asset
t.boolean :current_asset
t.boolean :current_liability
t.boolean :equity
t.integer :account_number
t.timestamps null: false
end
end
end
Account Model
class Account < ActiveRecord::Base
has_many :cashbook, foreign_key: :account_name
end
Account Controller
def new
#account = Account.new
end
def edit
end
def create
#account = Account.new(account_params)
end
def account_params
params.require(:account).permit(:account_name, :operating_expense, :cost_of_sales, :sales, :other_income, :non_current_liability, :non_current_asset, :current_asset, :current_liability, :equity, :account_number)
end
Cashbook db
class CreateCashbooks < ActiveRecord::Migration
def change
create_table :cashbooks do |t|
t.date :date
t.string :description
t.boolean :account
t.string :kind
t.integer :amount
t.timestamps null: false
end
end
end
You can use the collection_select form helper.
In your case, it could look something like
<%= collection_select(:cashbook, :account_name, Account.all, :account_name, :account_name) %>
Though you should also consider using the id column to associate models as per the Rails Associations Guide. If you change your models to use ids as the foreign key, you won't need to explicitly specify the foreign key in your has_many and belongs_to statements, and you can still create a select box that displays account names:
<%= collection_select(:cashbook, :account_id, Account.all, :id, :account_name) %>

ActiveModel::MissingAttributeError in Controller #create

I have a rails application with
1)User Model
class User < ActiveRecord::Base
has_secure_password
has_many :projects
end
2) Project Model
class Project < ActiveRecord::Base
belongs_to :user
end
3) CreateUser in db/migrate
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :email
t.string :password_digest
t.references :projects
t.timestamps null: false
end
end
end
4) CreateProject in db/migrarte
class CreateProjects < ActiveRecord::Migration
def change
create_table :projects do |t|
t.string :name
t.string :description
t.references :users
t.timestamps null: false
end
end
end
Now in my Controller, I have a function
def create
#project = Project.new(project_params)
#user = User.find(session[:user_id])
if #project.save
#user.projects << Project.find(#project.id)
redirect_to '/'
else
redirect_to '/project/create'
end
end
But when i call http://localhost:3000/project/new, I receive following error :-
-NoMethodError in ProjectController#create
-undefined method `projects' for # User
with
#user.projects << Project.find(#project.id)
highlighted in the extracted source.
Am I entering the record into has_many relationship correct, or is my syntax wrong?
I ran the following code in the console the server,
user = User.find(1)
user.projects
I received this error message:
NoMethodError: undefined method `projects' for #<User:0x00000001f5b508>
from /home/harshil/.rvm/gems/ruby-2.2.1/gems/activemodel-4.2.4/lib/active_model/attribute_methods.rb:433:in `method_missing'
from /home/harshil/.rvm/gems/ruby-2.2.1/gems/activemodel-4.2.4/lib/active_model/attribute_methods.rb:433:in `method_missing'
Thanks
It seems the CreateUser migration is incorrect. It should not reference the projects. Projects should reference the User, which you have done correctly.
I believe this is confusing ActiveRecord
try removing t.references :projects from the UserCreate migration and try again

Rails amoeba gem doesn't copy many-to-many relation

I have a problem with copying database records. I have a simple model User, that contains one-to-many relation with Language model and many-to-many relation with Skill model. I wanted to use amoeba gem to copy records with all associations. One-to-many copying works fine, but many-to-many doesn't copy at all.
Here's the code of User and Skill model:
user.rb
class User < ActiveRecord::Base
belongs_to :language
has_and_belongs_to_many :skills
validates_presence_of :email, :name
validates :email,
presence: { with: true, message: "cannot be empty" },
uniqueness: { with: true, message: "already exists in database" }
amoeba do
enable
end
end
skill.rb
class Skill < ActiveRecord::Base
has_and_belongs_to_many :users
end
I have also migration files that crates users, skills and skills_users tables:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name, null: false
t.string :email, null: false
t.references :language
t.timestamps null: false
end
end
end
.
class CreateSkills < ActiveRecord::Migration
def change
create_table :skills do |t|
t.string :name
t.timestamps null: false
end
end
end
.
class AddUsersSkillsTable < ActiveRecord::Migration
def change
create_table 'skills_users', :id => false do |t|
t.column :user_id, :integer
t.column :skill_id, :integer
end
end
end
Controller action 'show' in users_controller looks like this:
def copy
#user_copy = #user.dup
if #user_copy.save(validate: false)
redirect_to action: "index"
end
end
I tried copying relations in user.rb like this, but it didnt work:
amoeba do
enable
clone [:skills]
end
What may cause the problem?
Ok, I found the mistake. I wrote
#user_copy = #user.dup
in the controller file instead of
#user_copy = #user.amoeba_dup

Rails 2: joint multi table

Sorry, i'am newbie
I have database:
Migrate
-Mst_group tble
class CreateMstGroups < ActiveRecord::Migration
def self.up
create_table :mst_groups do |t|
t.string :group_name
end
end
end
-Mst_japan
class CreateMstJapans < ActiveRecord::Migration
def self.up
create_table :mst_japans do |t|
t.string :name_level
end
end
end
-Tbl_user
class CreateTblUsers < ActiveRecord::Migration
def self.up
create_table :tbl_users do |t|
t.string :login_name, :null =>false,:limit =>15
t.string :password,:null =>false,:limit =>50
t.string :full_name,:null =>false
t.string :full_name_kana
t.string :email,:null =>false
t.string :tel,:null =>false,:limit =>15
t.date :birthday,:null =>false
t.references :mst_groups
end
end
end
-Tbl_detail_user_japan
class CreateTblDetailUserJapans < ActiveRecord::Migration
def self.up
create_table :tbl_detail_user_japans do |t|
t.date :start_date
t.date :end_date
t.integer :total
t.references :tbl_users
t.references :mst_japans
end
end
end
Model
class MstGroup < ActiveRecord::Base
has_many :tbl_users
end
class MstJapan < ActiveRecord::Base
has_many :tbl_detail_user_japans
end
class TblUser < ActiveRecord::Base
belongs_to :mst_group
has_one :tbl_detail_user_japan
end
class TblDetailUserJapan < ActiveRecord::Base
belongs_to :tbl_user
belongs_to :mst_japan
end
Controller
def index
#user= ???????
end
How to write command select : login_name, full_name, full_name_kana, email, tel, group_name, name_lever, start_date, end_date, total in controller
It depends on how you want to retrieve the User object. You need to tell Rails how to find the TblUser object. If, for example, the user ID is known, let's say in a variable called 'id' then you would do:
def index
#user=TblUser.find(id)
end
It depends on your application logic how Rails would know which user you need. You may need an input from the user in case of log in, etc.
(Typically in Rails you would call the table 'Users', by convention tables and classes have the same name and then you wouldn't need to call the class TblUser)
That is all you need in the controller, you don't need to tell it which fields you want.
Then in the View you can access all the fields:
Fields on TblUser, example:
<%= #user.email %>
You can access the fields from related objects through the relations, example:
<%= #user.mst_group.group_name %>
Hope that helps to get you started.

Resources