I am having an issue with this controller Active record query. I have a model called Submissions and each Submission has an AgentActivity associated with it. I am trying to simply show all Submission fields on a row as well as associated Agent Activity next to it using a db query but I am getting errors with this, is that too simple?:
#agent_activities = #submissions.find(:agent_activity)
Submission index action
def index
#agent_activities = #submissions.find(:agent_activity)
#submissions =
SubmissionLocation.includes(:submission).where(location_id:
current_agent.Company_Business_Location).map(&:submission)
end
<% #submissions.each do |submission| %>
<tr>
<td><%= submission.First_Name %></td>
<td><%= submission.Last_Name %></td>
<% if user_signed_in? %>
<td><%= submission.Phone %></td>
<td><%= submission.Email %></td>
<%else%>
<% end %>
<td><%= submission.Desired_Location %></td>
<td><%= submission.number_of_beds %></td>
<td><%= submission.number_of_occupants %></td>
<td><%= submission.Rent_price_per_month_gbp %></td>
<% if user_signed_in? %>
<td><%= submission.Max_move_in_date %></td>
<td><%= submission.Tenant_Occupation %></td>
<td><%= submission.Contact_me_on %></td>
<td><%= submission.Furnished %></td>
<td><%= submission.Current_Address %></td>
<td><%= submission.Property_Requirements %></td>
<%else%>
<% end %>
<% Array(#agent_activities).each do |agent_activity|%>
<td><%= agent_activity.Status %></td>
<td><%= agent_activity.Notes %></td>
<% end%>
<td><%= link_to 'See More', submission %></td>
<% end %>
Submissions model
class Submission < ApplicationRecord
belongs_to :user, :optional => true
has_many :agent_activities, inverse_of: :submission
has_many :agents, through: :agent_activities
accepts_nested_attributes_for :agent_activities, :reject_if=> proc
{|attributes| attributes[:entry].blank?}, :allow_destroy => true
has_many :submission_locations
attr_accessor :location_1, :location_2, :location_3
end
Agent Activity model
class AgentActivity < ApplicationRecord
belongs_to :submission, :optional => true, inverse_of:
:agent_activities #has submission_id foreign key in table
belongs_to :agent, :optional => true, inverse_of: :agent_activities
end
You are assigning #submissions AFTER you are trying to call find on it. You need to reverse the order.
EDIT: Also you are using the association wrong. It doesnt look like you have a SubmissionLocation model.
Should be:
def index
#submissions = Submission.where() #fill this query out
end
And then when looping through submissions on the view you can call submission.agent_activities
Related
Model:
class Case < ActiveRecord::Base
belongs_to :employee, :foreign_key => :input_id
end
Controller
def index
#cases = Case.all
end
View
<% #cases.each do |c| %>
<tr>
<td><%= c.employee.name %></td>
<td><%= c.problem %></td>
<td><%= c.assigned_id %></td>
<td><%= c.solution %></td>
<td><%= c.verified_id %></td>
</tr>
<% end %>
How do I make the view display the employee name who owns the assigned_id?
(I'm already using employee.name to display the name for the input_id column?)
Use two reflections
class Case < ActiveRecord::Base
belongs_to :employee, :foreign_key => :input_id
belongs_to :assigned_employee, :foreign_key => : assigned_id, : class_name => "Employee"
end
In the View
<% #cases.each do |c| %>
<tr>
<td><%= c.employee.name %></td>
<td><%= c.assigned_employee.name %></td>
Trying to show individual items on my grocery app.
Using item.product.title & item.quantity.
because I want to do a subtotal of the orders. At the moment i get
NoMethodError in Orders#show - undefined method `product' for #<OrderItem:0x007fe53514d6e8>
orders.show.html
<h1>Your Order</h1>
<table class="table table-striped">
<tr>
<th>Customer</th>
<td><%= #order.user_id %></td>
</tr>
<tr>
<th>Status:</th>
<td><%= #order.status %></td>
</tr>
<tr>
<th>Items:</th>
<td><%= #order.order_items.count %></td>
</tr>
<tr>
<th>Items</th>
<th>Title</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Subtotal</th>
</tr>
<% #order.order_items.each do |item| %>
<tr>
<td></td>
<td><%= item.product.title %></td> <-----Error
<td><%= item.quantity %></td>
<td><%= item.product.price %></td>
<td><%= print_price item.subtotal %></td>
<% end %>
</table>
<%= link_to 'Edit', edit_order_path(#order) %> |
<%= link_to 'Back', orders_path %>
product.rb
class Product < ActiveRecord::Base
validates_numericality_of :price
validates :stock ,numericality: { greater_than_or_equal_to: 0 }
has_many :order_items
end
order.item
class OrderItem < ActiveRecord::Base
belongs_to :Order
belongs_to :Product
validates :order_id, :product_id, presence: true
end
full error
NameError in Orders#show
undefined local variable or method `product' for #<#<Class:0x007fe539003568>:0x007fe5391abd48>
app/views/orders/show.html.erb:30:in `block in _app_views_orders_show_html_erb__2160818799905733439_70311241143160'
app/views/orders/show.html.erb:27:in `_app_views_orders_show_html_erb__2160818799905733439_70311241143160'
Order item needs to be
class OrderItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
validates :order_id, :product_id, presence: true
end
Easiest way to think of it, is that belongs_to is defining the method that returns the object associated with the join. So :product - to create order_item.product. Not :Product
I have these models
class Course < ActiveRecord::Base
attr_accessible :name
has_many :teachers
end
class Teacher < ActiveRecord::Base
attr_accessible :id, :name, :course_id
belongs_to :course
has_many :evaluations
end
class Evaluation < ActiveRecord::Base
attr_accessible :teacher_id, :course_id
belongs_to :teacher
end
this is the views/evaluations/index.html.erb file
<% #evaluations.each do |evaluation| %>
<tr>
<td><%= evaluation.teacher_id %></td>
<td><%= link_to 'Show', evaluation %></td>
<td><%= link_to 'Edit', edit_evaluation_path(evaluation) %></td>
<td><%= link_to 'Destroy', evaluation, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td>
</tr>
<% end %>
I want to display the teacher's name with:
<td><%= evaluation.teacher.name %></td>
but it doesn't work.Rails shows this error:
"undefined method `name' for nil:NilClass"
Can anyone help me?
For all of your evaluation rails will get the teacher and then display it's name. If only one evaluation has no teacher, it will get nul for the teacher and then try to get name on that nil, then you have your error.
Try this :
<td><% if evaluation.teacher %>
<%= evaluation.teacher.name %>
<% end %></td>
I need to put fields from journals and journal_entries into one row on a table, and have the ability to add and show many data entry rows in the same view. (ie a table of rows and using link_to_add_fields with accepts_nested_attributes to expand the rows in the table).
There has to be some kind of f.parent.text_field or f.object.parent.text_field?
I'm trying to do something like the following
<table>
#in a :pm namespace
<%= form_for [:pm, #lease] do |f| %>
<%= f.fields_for :journal_entries do |journal_entries| %>
<%= render "journal_entry_fields" , f: journal_entries %>
<% end %>
<%= link_to_add_fields "+ Add transactions", f, :journal_entries %>
<% end %>
</table>
_journal_entry_fields.html.erb
<fieldset>
<tr>
## HERE IS WHAT I'M LOOKING FOR <<<<<<<<<<<!!>>>>>>>>>>>>>
<td><%= f.parent.text_field :dated %></td>
<td><%= f.parent.text_field :account_name %></td>
<td><%= f.text_field :credit %></td>
<td><%= f.text_field :notes %></td>
</tr>
</fieldset>
My Models
class Lease < ActiveRecord::Base
has_many :journals, :order => [:dated, :id] #, :conditions => "journals.lease_id = id"
has_many :journal_entries, :through => :journals
accepts_nested_attributes_for :journal_entries , :allow_destroy => true
accepts_nested_attributes_for :journals , :allow_destroy => true
end
class Journal < ActiveRecord::Base
belongs_to :lease, :conditions => :lease_id != nil
has_many :journal_entries
accepts_nested_attributes_for :journal_entries , :allow_destroy => true
end
class JournalEntry < ActiveRecord::Base
belongs_to :journal
end
I'm using Rails 3.2.12 and ruby 1.9.3
I'm trying to see if this is a better solution than the problem faced on: rails link_to_add_fields not adding fields with has_many :through (with nested form inside)
I made a different thread because I think it's so drastically different.
Thanks,
Phil
As per my understanding about your use-case you want to create journals and its entries in a single form of Lease. So, you can us the fields_for for both of them as below:
<table>
#in a :pm namespace
<%= form_for [:pm, #lease] do |f| %>
<%= f.fields_for :journals do |journal| %>
<%= render "journal_entry_fields" , f: journal %>
<% end %>
<%= link_to_add_fields "+ Add transactions", f, :journals %>
<% end %>
</table>
_journal_entry_fields.html.erb
<fieldset>
<tr>
<td><%= f.text_field :dated %></td>
<td><%= f.text_field :account_name %></td>
<%= f.fields_for :journal_entries do |journal_entry| %>
<td><%= journal_entry.text_field :credit %></td>
<td><%= journal_entry.text_field :notes %></td>
<% end %>
</tr>
</fieldset>
Though you need to initialise the journal entries every time a new record is added dynamically. I can't help you with this right now as I'm not on my PC.
Try this: http://railscasts.com/episodes/196-nested-model-form-revised
The RailsCasts model relations are similar to your model relations, although you would need to alter the HTML.
RailsCasts Models: Survey > Question > Answer
Your Models: Lease > Journal > JournalEntry
I have an authors page that displays all the authors in the database.
<h1>Listing authors</h1>
<table>
<tr>
<th>Name</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #authors.each do |author| %>
<tr>
<td><%= author.name %></td>
<td><%= link_to 'Show', author %></td>
<td><%= link_to 'Edit', edit_author_path(author) %></td>
<td><%= link_to 'Destroy', author, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<%= link_to 'New Author', new_author_path %>
And for each author, you click show, to bring up their own page.
<p>
<b>Name:</b>
<%= #author.name %>
</p>
<%= link_to 'Edit', edit_author_path(#author) %> |
<%= link_to 'Back', authors_path %>
Now I have the same set up for books, where the user can enter new books, show and edit books in the database.
I then set up a model called authorbooks that holds a relation ship between authors and books using has_many, and belongs_to in the models for author.rb, book.rb and authorbook.rb.
I am wanting the show page of the author to display every book that they are related to.
How would I go about this? I am new to rails and still learning so please remember when answering. Thanks in advance.
EDIT model code for each model:
author.rb
class Author < ActiveRecord::Base
attr_accessible :name
validates :name, :presence => true
has_many :authorbooks
has_many :books, :through => :authorbooks
end
book.rb
class Book < ActiveRecord::Base
attr_accessible :name
validates :name, :presence => true
has_many :authorbooks
has_many :authors, :through => :authorbooks
end
authorbook.rb
class Authorbook < ActiveRecord::Base
attr_accessible :author_id, :book_id
belongs_to :book
belongs_to :author
end
It would have been interesting to see the model code as well. I assume you have something like:
class Author
has_many :author_books
has_many :books, :through => :author_books # this line might be missing,
# read in the api documentation about it.
class AuthorBooks
belongs_to :author
belongs_to :book
Now you can do something like:
<h3>Related books</h3>
<ul>
<% #author.books.each do |book| %>
<li><%= book.name %> <%= link_to "Details", book_path(book) %></li>
<% end %>
</ul>
Without the :through line you could have done something like:
#author.author_books.each do |ab|
... ab.book.name ...
Note 1: you get N+1 load problems with the second example. See the eager loading chapter in A::R guide for more information about that.
Note 2: Checkout HAML; much nicer than ERB