Having an invoice model which belongs to user, and a user table that associates a user as a particular role or multiple roles:
class CreateInvoices < ActiveRecord::Migration
def self.up
create_table :invoices do |t|
t.string :po_number
t.datetime :invoice_date
t.datetime :date_received
t.datetime :date_approved
t.text :clerk_note
t.integer :clerk_id
t.integer :approver_id
t.text :approver_note
end
end
class Invoice < ActiveRecord::Base
belongs_to :user
end
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :username
t.string :email
t.boolean :account_clerk
t.boolean :approver
t.boolean :admin
end
end
class User < ActiveRecord::Base
has_many :invoices
end
In the invoice record, how do I assign an clerk_id and approver_id depending on the role set in the user model? Each invoice will have a clerk and approver, but both are users.
Likewise, how do I assign the clerk_note to the clerk_id and the approver_note to the approver_id? I'm assuming in this case, I can just refer to current_user, since the user logged in will be the one making the note, right?
I'm not sure what this is called, so in my Googling, I didn't know what to look for... thanks in advance for your help.
To answer your original question using your current models you can just create your view to display different fields based on the role of the user (this assumes the logged in user is in an instance variable named #user).
<% if (#user.approver) then %>
<%= f.text_area :approver_note %>
<% else %>
<%= f.text_area :clerk_note %>
<% end %>
Then in your controller you can set the value of clerk id or approver id again depending on the logged in user.
if (#user.approver) then
#invoice.approver_id = #user.id
else
#invoice.clerk_id = #user.id
end
Related
Model
class Assembly < ApplicationRecord
belongs_to :book
has_and_belongs_to_many :parts
end
Join table
class CreateJoinTableAssemblyPart < ActiveRecord::Migration[7.0]
def change
create_join_table :assemblies, :parts, id: false do |t|
t.index [:assembly_id, :part_id]
t.index [:part_id, :assembly_id]
end
end
end
Schema
create_table "assemblies_parts", id: false, force: :cascade do |t|
t.integer "assembly_id", null: false
t.integer "part_id", null: false
t.integer "assemblies"
t.integer "parts"
end
Controller
def create
#assembly = Assembly.new(assembly_params)
def assembly_params
params.require(:assembly).permit(:book_id, :part_id)
_Form.html
<div class="form-inputs">
<%= f.association :book %>
<%= f.association :parts %>
</div>
This way saves only the book_id
I need to save the part_id but it doesn't save and it doesn't even give an error
You need to whitelist the correct param key and an array of permitted scalar values - not a single value:
def assembly_params
params.require(:assembly)
.permit(:book_id, part_ids: [])
end
Check the logs for the correct param key to use.
Also remove the assemblies and parts columns from the assemblies_parts table. You don't need them and it just seems like an open invitation for bugs.
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) %>
I have a single user table with two roles defined using enums and Single Table Inheritance with two matching user sub classes for Staff and Clinician. Because I need a lot of information about clinicians that I don't need about Staff, I've created a clinician_profiles table and am using after_create :create_clinician_profile in the user model to create a stub for the clinician profile.
I am trying to build a page for clinicians to complete their profiles and am having trouble getting the instance variable defined in the controller so it pulls the appropriate clinician's profile into the view to render the form. I am getting the following error. Note that the user's id I'm logged in as is 104.
ActiveRecord::RecordNotFound in ClinicianProfilesController#edit
Couldn't find ClinicianProfile with 'id'=104
I feel like I'm close but don't know what I'm doing wrong. Any help would be greatly appreciated for a rookie!
I'm landing on the page using this link in the header:
<li><%= link_to "Manage My Profile", edit_clinician_profile_path(current_user) %></li>
Here are relevant parts of my user model:
class User < ApplicationRecord
self.inheritance_column = :role
enum role: { Staff: 0, Clinician: 1}
belongs_to :university
has_many :referral_requests
class Staff < User
validates :university_id, presence: true
end
class Clinician < User
has_many :lists
has_many :universities, through: :lists
has_one :clinician_profile
after_create :create_clinician_profile
end
Here's my ClinicianProfile model:
class ClinicianProfile < ApplicationRecord
has_many :clinician_profile_languages
has_many :languages, through: :clinician_profile_languages
has_many :clinician_profile_races
has_many :races, through: :clinician_profile_races
belongs_to :clinician
end
clinician_profiles schema
create_table "clinician_profiles", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "firstname"
t.string "lastname"
t.string "address1"
t.string "address2"
t.string "city"
t.string "state"
t.string "zip"
t.boolean "accepting_patients"
t.integer "rate"
t.string "license_number"
t.string "license_state"
t.string "school"
t.integer "year_graduated"
t.string "accepts_insurance"
t.boolean "sliding_scale"
t.text "bio"
t.boolean "verified"
t.integer "years_licensed"
t.integer "years_of_experience"
t.integer "clinician_id"
end
ClinicianProfilesController
class ClinicianProfilesController < ApplicationController
def edit
#clinician_profile = ClinicianProfile.find(params[:id])
end
def index
end
def show
end
def create
end
end
Here's the beginning of my view (edit.html.erb in /views/clinician_profiles)
<%= form_for(#clinician_profile) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
def edit
#clinician_profile = ClinicianProfile.find(params[:id])
redirect_to :index and return unless #clinician_profile
end
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.
I'm new to rails and and I'm on the urge of learning Associations.
I'm using Rails version 3.
I have a user model and post model.My need is as below:-
Models
class User < ActiveRecord::Base
has_many :post
end
class Post < ActiveRecord::Base
belongs_to :user
validates_associated :user
end
Schema
ActiveRecord::Schema.define(:version => 20101016171256) do
create_table "posts", :force => true do |t|
t.integer "sell_or_buy"
t.string "title"
t.text "body"
t.integer "user_id" <<<<<<< I thought this will help to associate to user model.
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.string "password"
t.integer "rank"
t.datetime "created_at"
t.datetime "updated_at"
end
end
I thought keeping a user_id field and the belongs_to association will do my job, but
when i tried to display all the posts belonging to a user as follows:
<%= #user.posts %>
in my show.html.erb file. But I get only the following display:-
Name: saran
Email: saran.saran007#gmail.com
Password: abcd
Rank:
Edit | Back
Posts
#<Post:0xb69f47f8>#<Post:0xb69f3024>
I want to display the associated posts "title" and "body" in a readable format.
Also I'm able to create a post with a user_id in which no user exists!. The validates_associated :user is also not working, Please help me out.
Its
class User
has_many :posts
end
Not
has_many :post
Edit and Update your results.
You are getting the posts as expected in your view... So I'm not sure I understand that part of your question. As to the other part, validates_associated just ensures that the attached object is valid itself, and not if it exists at all. For that you want validates_presence_of. See the docs.
I wrote the following partial for my purpose and it works well :).
Thanks for all your inputs.
<% for post in #user.posts do %>
<h3> <%= post.title %> </h3>
<%= post.body %>
<% end %>