Filtering Rails database - ruby-on-rails

I have created a database of researchers and the papers they have written in Rails. I need to be able to filter it by author in such a way that information is drawn from the database onto a page in the persons name (for example if there is a researcher called Dr. A. Researcher, I need to be able to go to his page, and that page will be populated with papers that he/she has written automatically). I have been working on this for a while now, and have gone round in circles so many times, Im not exactly sure what I am looking for (though I think its probably going to be AJAX based). Filtering the database seems quite straightforward, but I cant seem to find any information about sending the results of that filter to a specific page.
As I say, Im not totally sure what Im looking for here, so someone may have had a similar issue that has already been answered. If that is the case, I apologise. Any help anyone has about this would be very gratefully received.
Cheers

Seems like you would just want to use the show action.
In your controllers/researchers.rb controller add
def show
#reseacher = Reseacher.find(params[:id])
end
or possibly
def show
#reseacher = Reseacher.find(params[:id], include: [:papers])
end
then in your views/show.html.erb view you can just add
<% #reseacher.papers.each do |p| %>
<%= p.title %>
<%= p.otherattribute %>
<% end %>
Also you will need to ensure your relationships are setup correctly in your model
in models/researcher.rb
add
has_many :papers
to the Researcher Class
and in models/papers.rb
add
belongs_to :researcher
to the Paper Class
localhost:3000/reseachers/1 where 1 is the id of the researcher should then return the papers for that researcher

Related

Showing attributes from a different rails model

I know this is probably an extremely basic level question, but I'm new to rails and can't seem to locate a clear answer in the Ruby Guides on my own; it's likely that I just don't know the term for this and can't figure it out.
I've got two models, documents and companies (companies is a table built by devise). Companies has_many :documents and documents belongs_to :companies. On my form there is a place for the company's name, address, etc., and I would like to populate the associated company on both on the _form.html.erb and the show.html.erb so that it's not necessary to input this information every time you fill out a form. It's not absolutely necessary that the information be present on the _form.html.erb, but it would be nice to go ahead and present this information so as to not confuse the user.
When I try calling #companies.company_name in my documents show view, I hit a nil class error. I've tried adding #companies = companies.all to my controller, but that doesn't work either. Like I said, I'm sure this is a simple problem, but I don't have much hair left and would like to preserve it for another problem.
I was able to find a different method that seems to be working well at the moment. Instead of adding #company = Company.find (params[:id]), I was able to call #document.company.company_name within the show action. I'll forgo to the new and edit for the moment since show was all that mattered.

Need a hand with a rails join

In my app, I have four models - Users > Clients > Jobs > Tasks
I have all of the associations set up fine (as per here --> Advice on RoR database schema and associations), but I'm stuck with a query.
Basically, what I'm doing is:
#potentialjobs = current_user.jobs.where(:status => "potential")
But in the results, I also want to include the client attrs in addition to the job attrs.
Can anyone point me in the right direction?
Thanks.
UPDATE
Appreciate the suggestions below.
I thought this should have been pretty straightforward, but I'm beginning to think the problem must lie somewhere else in my codebase.
Whenever I try to include or join the client model in this query or as a second-order association in the model itself, I get the following error:
uninitialized constant Job::Clients
...and I'm not sure why. So, can anyone shed some light on this for me?
ANOTHER UPDATE
Solved - I had a pluralisation error in one of my models. Gah. Thanks all.
You can access the client's fields through the association in both ways.
<% #potential_jobs.each do |job| %>
<%= job.client.name %>
<% end %>

RoR query condition: does not contain any of a list of attributes

I feel like there should be a simple answer for this question, but I haven't been able to manage.
I have 3 models: User, Course, and Textbook. My model associations are working just fine, but I will include them if necessary. In my application, I want a view to display the courses for which the user owns textbooks and all other courses for which the user does NOT own the textbooks. In pseudocode, it should look like
#courses_with_books = current_user.textbooks
#courses_without_books = current_user.courses.where(:id != #courses_with_books.course_ids)
I can complete that second line using complicated loops, but the processes seemed silly so I've been researching a way to gather the information I need with a single query and have yet come up with nothing. Thanks in advance :D
In general, if you want to check that some attribute is not inside of some given collection, try 'NOT IN':
current_user.courses.where("id NOT IN ?", #courses_with_books.course_ids)
For more on Active Record Queries, check out the Active Record Query Interface Guide.
rathrio is on the right path. I think for the first part you could do
#courses_with_books = current_user.textbooks.collect(&:course)
If you're trying to display their titles, do
<% #courses_with_books.each do |cwb| %>
<%= cwb.title %>
<% end %>

Collection_select has always only 1 item, not an array? In ruby on rails

I am implementing in Ruby on Rails and I am trying to work with the collection_select, I'm a newbie. I just want to do, I have a list with groups and a list with roles. These are both models. So, I list my groups, and next to that, I have a dropdown list with the role for the group. each group has 1 role.
I implemented some code already, but the collection_select always only remembers the last item. So I want a list with groups, connected with the desired role. But, now I only have 1 item. This is my view:
<% #groups.each do |group| %>
<li>
<%= collection_select('group', 'role_id', #roles, 'id', 'name') %>
</li>
<% end %>
I don't really know what to do now? Someone who knows what I am doing wrong?
Thanks
So, I assume that you're doing a form? What model does the form belong to?
To help debug this sort of thing, usually it'd be a good idea to check your development.log file to see what parameters the form is passing to the controller. Something like:
Parameters: {"commit"=>"Save", "action"=>"update", "_method"=>"put",
"id"=>"6168", "group"=>{"role_id"=>"2", ...}, "controller"=>"groups"}
Now, usually a Rails controller is expecting a form with the data for a single model. If you're wanting to update multiple models or rows at the same time, you're going to have to get creative.
First thing to do might be to try returning an array of groups. Your form at the moment is not using an array. I doubt that these Rails helpers will help you though. Helpers like these are designed to update one ActiveRecord object at once.
It's possible you may need to rethink the design of your app to better fit the Rails way, or roll your own form and iterate over the array that it passes through. Doing it the Rails way is the recommended option, it just might take some brain bending from your end to figure that part out. If you need help, maybe provide more information on what you're actually trying to achieve.

Complex relationships in Ruby - display data across many tables

I have designed an application consisting of 20+ tables, and utilizing Ruby on Rails.
I am using an authentication system for users to login. I want to display to the user, when he logs in, information related to him that can be found across many tables, between these tables there are all the possible relationships. How can I accomplish this in RoR? Should I consider using views, multiple joins? I did not managed to get information from one model further than 2 tables looking in the tables design using classical approaches like "has many", ":through" etc.
Can you please share your knowledge?
Thanks!
I assume that your User model has relationships with all the tables that contain related information.
Assuming that, you can have an action in UsersController and a corresponding view. In the action you can find out who is the currently connected user, and then get all the data you need from the related models. Return these data to the view and render them as you like.
I think your question is too general, but here are some thoughts...assuming you are making a conventional Rails app
"Should I consider using views?"
Views are what users see in conventional rails apps. So if you want to display data to your users, use views to display data that is made available in the controllers. Each action in the controller will grab data from the models, and have a corresponding view.
So to display books that belong to a user, you might have a Books controller. After a user logs in, you have an "index" action in the "Books" controller that says #user_books = #user.books. In this case, #user would be the user object from your authentication system. A user would have many books, and it is possible that a book would have many users. Then in your views/books/index.html.erb file you have something like:
<ul>
<% #user_books.each do |b| %>
<li> <%= b.name %> </li>
<% end %>
</ul>
to print a list of book names.
Joins are done automagically by Rails, so you don't have to worry about it quite yet. But if your app gets really big and queries are complicated, you may have to rewrite the SQL statements to improve scalability....but that is obviously a good problem to have.
Hope this helps!

Resources