I've got a simple one to many relationship between tasks and priorities.
class Task < ActiveRecord::Base
attr_accessible :subject, :body, :priority_id, :company_id, :status_id, :user_ids
has_and_belongs_to_many :users
belongs_to :priority
belongs_to :company
belongs_to :status
end
class Priority < ActiveRecord::Base
attr_accessible :name
has_many :tasks
end
From my tasks/show.html.erb view
<%= #task.priority.name %>
This works.
However in my tasks/index.html.erb
<% #tasks.each do |task| %>
<tr>
<td><%= task.subject %></td>
<td><%= task.body %></td>
<td><%= task.priority.name %></td>
<td><%= link_to 'Show', task %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Destroy', task, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
The call to task.priority.name does not work.
It throws a NoMethodError.
I'm guessing this is a really stupid newbie mistake but I can't for the life of me figure it out. It would have assumed that the Active Record magic would have filtered down into a look like this.
I guess your NoMethodError comes on nil:NilClass. Try:
<%= task.priority.name if task.priority %>
Are you sure you have well created all the models and links with primary keys in your database? I think you link to a task with a priority id (prority_id) which doesn't exists.
Check your datas.
Given that there is no validation of the presence of priority in task, it's quite possible that you are calling "name" on nil. Validate the presence of priority (with validates_presence_of) or check it exists before printing it as Christoph says.
Related
I am using Devise gem for user Authentication. User id is as foreign key in article table
How can i get writer name through User_id in a view Show_article.html.erb
I can access user_id in show_article.htmlerb
I have tried to make a custom function in article controller but could not get the desired output
your Article model should look like this:
class Article << ActiveRecord::Base
belongs_to :user
#....some more lines
end
your User model should look like this:
class User << ActiveRecord::Base
has_many :articles
#....some more lines
end
and your show.html.erb should say:
#....your rest of code
<%= #article.try(:user).try(:name) %>
#....your rest of code
This will skip user name if you article doesn't have any user. It looks like your article doesn't have user_id or you haven't defined your relations correctly.
It might be the case that some of your article do not have any user. so
you can do
<%= #article&.user&.name %> in show page
and in the index page
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= article&.user&.name%></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Delete', article_path(article),
method: :delete,
data: { confirm: 'Are you sure you want to delete this article?' } %></td>
</tr>
<% end %>
but make sure you have ruby 2.3.0 or higher
I have two tables
sample
has_many :abundances
self.primary_key = :sample_id
and
abundance
has_many :samples
self.primary_key = :sample_id
In abundances controller i have a ransack search
def index
#search = Abundance.ransack(params[:q])
#abundances = #search.result
#abundancez = #abundances.paginate(:page => params[:page],:per_page => 100)
end
end
in the abundances view, I have a filtered table based on the ransack parameters.
<%= will_paginate #abundancez%>
<% #abundancez.each do |abundance| %>
<td><%= abundance.sample_id %><td>
<td><%= abundance.length %><td>
<td><%= abundance.eff_length%><td>
<td><%= abundance.est_counts%><td>
<td><%= abundance.tpm%><td>
<% end %>
The sample table has a field, race that i want to pull up in the abundances view when via corresponding to the filtered parameters above.
Ive tried to use pluck in the view
<%= will_paginate #abundancez%>
<% #abundancez.each do |abundance| %>
<td><%= abundance.sample_id %><td>
<td><%= abundance.length %><td>
<td><%= abundance.eff_length%><td>
<td><%= abundance.est_counts%><td>
<td><%= abundance.tpm%><td>
<td><%= samples.pluck(abundance.samples_id,:race)%></td>
but i get an error. Not sure if I'm going about this the right way or if I the syntax is incorrect.
Thanks!
The thing here is that you defined that an abundance has_many samples, so you can't return the value of the race for one sample like you are doing here.
as you said in your comments, it seems that your associations are wrong, change your abundance model from has_may, to belongs_to
belongs_to :sample
and then on your view, you can return the race value like this
<%= will_paginate #abundancez%>
<% #abundancez.each do |abundance| %>
<td><%= abundance.sample_id %><td>
<td><%= abundance.length %><td>
<td><%= abundance.eff_length%><td>
<td><%= abundance.est_counts%><td>
<td><%= abundance.tpm%><td>
<td><%= abundance.sample.race %></td>
this because you said in the comments that abundances have a sample_id attribute, so abundance belongs_to a sample. of course this will work if abundance have the attribute of sample_id filled with a valid ID (an existing one sample) and of course, that it has a race value.
I'm looking to use the restrict_with_error validation in Rails 4.2.
In this application, an area has multiple apartments. I want to prevent area from being deleted if it has apartments associated with it.
Here is the area model:
class Area < ActiveRecord::Base
has_many :apartments, :dependent => :restrict_with_error
validates :name, presence: true
end
Here is the view where the user can delete an area:
<% #areas.each do |a| %>
<tr>
<td><%= a.id %></td>
<td><%= a.name %></td>
<td><%= a.notes %></td>
<td><%= link_to 'Destroy', a, method: :delete, data: { confirm: 'Are you sure you want to delete this area?' } %></td>
</tr>
<% end %>
</tbody>
</table>
If I try to delete an area that has associated apartments, it is restricted. However, no error is displayed.
This might be a very simple question, but where is the error displayed if the deletion is restricted?
Thanks so much in advance!
You need to access the area errors. You can do so in the area controller like this:
def destroy
unless #area.destroy
flash[:notice] = #area.errors.full_messages[0]
end
redirect_to areas_path
end
I have a Submitter Model which has an email column. I also have an Email Model that has email suffix column in it.
My issue (I believe) is that since my Submitter Model has an email column, when I try to use submitter.email.suffix to display the suffix in my Email model it says there's no defined method.
Any idea what I can do here?
View:
<% #submitters.each do |submitter| %> <tr>
<td><%= submitter.school.name %></td>
<td><%= submitter.first_name %></td>
<td><%= submitter.last_name %></td>
<td><%= submitter.email %><%= submitter.email.suffix %></td>
<td><%= link_to 'Show', submitter %></td>
<td><%= link_to 'Edit', edit_submitter_path(submitter) %></td>
<td><%= link_to 'Destroy', submitter, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
As a side note I have no problem showing the IDs of the emails suffix but obviously I'm trying to show the actual suffix instead:
<td><%= submitter.email %><%= submitter.email_suffix_id %></td>
Submitter.rb has a has many :emails and Email.rb has a belongs_to :submitter association.
So let's break this down. You say you're trying to do
submitter.email.suffix
however, your model has no such :email association. Instead you've stated your Submitter model has
has_many :emails
This would mean:
To print the :email attribute on some Submitter instance submitter, use
puts submitter.email
To print the :prefix attribute of each Email instance related to submitter via your has_many association, you'd need to loop over them
submitter.emails.each do |e|
puts e.suffix
done
It's still quite unclear what exactly you're trying to do, but hopefully this clears up some confusion on your end; there is no method naming conflict so far.
submitter.email.suffix won't work because it returns Submitter "email" which is a string, not the Email instance.
submitter.emails.each(&:suffix)
should work. submitter.emails will return an Email's instances and they do have suffix method(according to your words).
I'm trying to pick out an instance of a model ("Package") by its relation to two other models to which it belongs ("Cut" and "Animal"). While there are, say many packages with :cut_id 3 and many with :animal_id 4, there should only be one with both, and I want to pick that one out and display its contents in a table.
I've tried the following DIY mess, and it's not really working. (cutfind is a method I created that I know works for calling out all of the cuts associated with the given animal.)
<% #animal.cutfind.each do |cut| %>
<tr>
<td><%= cut.name %></td>
<td><%= number_to_currency(cut.price) %></td>
<td><%= cut.package_weight %> lb</td>
<% #a = Package.where(:animal_id => #animal.id) %>
<% #pset = #a.where(:cut_id => cut.id) %>
<% #pset.each do |p| %>
<td><%= p.original %></td>
<td><%= p.left %></td>
<% end %>
</tr>
<%end%>
Any idea how to do this [better]? Thanks.
Update: I tried this other DIY mess and am getting the same problem (the cells aren't even being created, which leads me to believe that #pset is empty).
This is in my animal model:
def packagefind
Package.where(:animal_id => self.id)
end
And then I changed the above like so:
<td><%= cut.package_weight %> lb</td>
<% #pset = #animal.packagefind.where(:cut_id => cut.id) %>
<% #pset.each do |p| %>
<td><%= p.original %></td>
<td><%= p.left %></td>
<% end %>
Rails will automatically generate methods to help you find the associated records if you define the following relations:
class Animal
has_many :cuts
has_many :packages, :through => :cuts
end
class Cut
belongs_to :animal
belongs_to :package
end
class Package
has_many :cuts
has_many :animals, :through => :cuts
end
In your controller, the following line will eager load all the records you will need in your view:
#animal = Animal.includes(:cuts => :package)
Your view can then be shortened to:
<% #animal.cuts.each do |cut| %>
<tr>
<td><%= cut.name %></td>
<td><%= number_to_currency(cut.price) %></td>
<td><%= cut.package_weight %> lb</td>
<td><%= cut.package.original %></td>
<td><%= cut.package.left %></td>
</tr>
<%end%>
As I'm not able to comment on your post, I take a guess:
You have the folllowing architecture:
Cut -> Package <- Animal
In this, "->" and "<-" are one-to-many relationships so that
class Package < ActiveRecord::Base
has_many :cuts
has_many :animals
end
So, you want "the" package, that has Cut with id 3 and Animal id 4.
Did you try:
x = Product.select { |product| product.cuts.include?(Cut.find(3)) }.select{ |product| product.animals.include?(Animal.find(4)) }
?
EDIT: I first suggested to you use
Product.find_by_product_id_and_animal_id()
which didn't work but showed the OP the way to do it