email Model and email column clashing in Rails - ruby-on-rails

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).

Related

How can i get User name in a table where user_id is used as foreign key

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

Rails resources routes miss parameters yet match documentation

Using Rails' resources directive, I have created a set of routes for a controller (contacts). I'm now editing the default views they come with to include some of my own content. However, the link_to method is failing, telling me that I'm missing a required parameter.
No route matches {:action => 'show', :controller => 'contacts', :id => nil} missing required keys [:id]
It's obvious why this is happening - the link_to method is not being supplied with an ID, instead it's getting nil. However, the code I'm using matches the documentation for link_to.
This is the view in question:
<% #contacts.each do |contact| %>
<tr>
<td><%= contact.first %></td>
<td><%= contact.last %></td>
<td><%= contact.title %></td>
<td><%= contact.city %></td>
<td><%= contact.phone %></td>
<td><%= contact.email %></td>
<td><%= link_to 'Show', contact %></td>
<td><%= link_to 'Edit', edit_contact_path(contact) %></td>
<td><%= link_to 'Delete', contact.id, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
#contacts is a set of contacts returned from the controller. The line that sets that is:
#contacts = Contact.select("title, first, last, city, phone, email")
.where("created_by" => #current_user.id)
The relevant content of the routes.rb file is simply resources :contacts.
The documentation states:
Because it relies on url_for, link_to supports both older-style controller/action/id arguments and newer RESTful routes. Current Rails style favors RESTful routes whenever possible, so base your application on resources and use [...]
link_to "Profile", #profile
This appears to be what I'm using with link_to 'Show', contact.
Why is the ID not getting passed to link_to?
What can I do to remedy this?
Change
Contact.select("title, first, last, city, phone, email")
to
Contact.select("title, first, last, city, phone, email, id")
the contact's id is nil because it isn't in the select query.
Also, although it doesn't seem to be causing problems right now, I would reccomend using an array of symbols instead of a comma-separated string, so that the sql query is more specific. For example:
Contact.select("title, first").to_sql #=> SELECT title, first FROM contacts
Contact.select(:title,:first).to_sql #=> SELECT "contacts"."title", "contacts"."first" FROM contacts
This way if you do a join with another model, it won't complain about the unspecific id in select. If you feel like you're typing too much, you can use the %i(...) syntax:
Contact.select(*%i(title first last city phone email))
Your code seems fine.
Maybe you have an instance in the #contacts array that is not saved and therefore, has no id?
Another way to put the same (again, your code is fine) would be:
= link_to 'Show', contact_path(contact)
I would suggest posting the routes file.

undefined method `name' for nil:NilClass

I have this problem on te line
<td><%= h box.manufacturer.name %></td>
of
<% #boxes.each do |box| %>
<tr>
<td><%= h box.manufacturer.name %></td>
<td><%= h box.model %></td>
<td><%= link_to 'Mostrar', :action => 'show', :id => box %></td>
<td><%= link_to 'Editar', :action => 'edit', :id => box %></td>
<td><%= button_to 'Eliminar', { :action => 'destroy', :id => box },
:confirm => "Are you sure you want to delete box #{box.model}?" %></td>
manufacturers is a table with field called name,its supposed that an object manufacturer has member called name ,isn it?
It's quite clear one of your box records doesn't have its associated manufacturer. If it's acceptable, you can use Object#try method, like this:
<%= box.manufacturer.try(:name) %>
If it's not, you should think of adding proper validation to Box model:
validates_presence_of :manufacturer
I didn't use h helper because in Rails >= 3.0 (which you probably use) untrusted content is escaped automatically.
It means your one of your boxes doesn't have a manufacturer.
You can prevent an exception like this by using try:
box.try(:manufacturer).try(:name)
If the manufacturer doesn't exist, it will return nil instead of throwing an exception
Yes, but in your case you seem to have a box with no manufacturer.
<%= h box.manufacturer.name if box.manufacturer %>
Or
<%= h box.manufacturer.try(:name) %>
The proposed solutions fix the issue but don't address the bug in architecture. I recommend learning the Tell Don't Ask principle.
Your views shouldn't do any checks on nil as it is not their responsibility. You should always try to avoid long method chains as it will always fail on certain step.
Refactor your application like this:
box.rb:
def manufacturer_name
manufacturer.try(:name)
end
view:
<td><%= h box.manufacturer_name %></td>
I don't think it's possible if the box is nil. If it is - then I recommend removing nils from the array before iterating over it in views like this:
controller:
#boxes = box_retrieving_method_with_nils.compact

Only displaying certain entries from a log based on id

I have a log in my webapp where you can input hours, and when you input your hours it automatically takes the account you are logged into(built using devise and cancan), and finds what your user_id is and tacks it on to an hour log.
Now what I cant seem to find out is how I can go about making it so that it only displays logs with your user_id. Is there a way to do this in the model or controller instead of the view?
This is the view code as of now.
<% #time_sheets.each do |time_sheet| %>
<tr>
<td><%= time_sheet.user_id %></td>
<td><%= time_sheet.day %></td>
<td><%= time_sheet.hours_worked %></td>
<td><%= time_sheet.minutes_worked %></td>
<td><%= link_to 'Show', time_sheet %></td>
<td><%= link_to 'Edit', edit_time_sheet_path(time_sheet) %></td>
<td><%= link_to 'Destroy', time_sheet, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
It shows the logs of everyone in the system instead of just that user.
Devise gives you a helper method named current_user. In your controller you can filter by user ID using the current_user.id. Like so:
#time_sheets = TimeSheet.where(:user_id => current_user.id).all
And that will give you only the currently logged in user's time sheets.

Rails - Accessing one to many variables in a table loop

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.

Resources