Order objects by external attributes in rails - ruby-on-rails-3

I have a user which belongs to a company.
I have a table with
<%= user.company.name %>
<%= user.name %>
etc.
Do you know how it is possible to sort my objects by the company name ?
I use sortable method from railscast : http://railscasts.com/episodes/228-sortable-table-columns
And I really don't know how to sort by company name...
Thanks for the help.

#users.sort! { |a,b| a.company.name.downcase <=> b.company.name.downcase }

Related

Show only onfollow softwares in library SQL Rails

I have a little problem with my first Ruby on Rails app.
I have 3 tables (Software, User and Library), a user can follow a software and see its follows in his library. But i want to see only no follows in his library.
I have no problem to see the follow but when I want to see only the softwares not followed by USER, i can not do it ...
Software.left_outer_joins(:libraries).where(libraries: {software_id: nil})
This code shows me all software that does not follow, but I need this information BY USER.
And this show follow softwares
#library_softwares = current_user.library_additions
With index.html
<% if #library_softwares.exists? %>
...
<%end>
Do you have an idea ?
Sorry for my english
Thx
EDIT :
I would like to reach this result:
#library_softwares = current_user.library_additions
#software = Software.all
#result = #software - #library_softwares
I hope I have understood what you want, saves the software information in a variable
#softwares = Software.left_outer_joins(:libraries).where(libraries: {software_id: nil})
and if you have a relationship between the user and the softawre you can get user information simply by going through the array of #softwares
<% unless #softwares.nil? %>
<% #softwares.each do |software| %>
<% software.user %>
<%end %>
<%end%>

rails 3 select control from nested models

I have 2 models user and team. a team can have many users each users belong to a team.
team_id is a column in my user table as a fk.
I want to have a select drop down on my create user form that has all my teams team_id in it.
This code does what I want BUT
<%= f.fields_for :teams do |builder| %>
<p>
<%= f.label :team_id, "Select Team For User" %>
<%= builder.collection_select :team_initials, Team.all, :id, :team_initials %>
</p>
<% end %>
it does not try to add the id value to a user. here is the error
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"xxxxx",
"user"=>{"name"=>"brand new",
"email"=>"test1#test.com",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"teams"=>{"team_initials"=>"27"}},
"commit"=>"Create my account"}
It complains that it cant add to the team model.
All I want to do is grab the ID of all existing teams for my create user form so when you crate a user you can select that users team.
The reason I have team_initials above is I was trying to use a more user friendly field for select but still user team Id as the value.
I think you're on the right track. The fields_for mechanism is for setting values in the associated model. I think all you're trying to do is make the association.
So you can lose the fields_for line and then this section:
builder.collection_select :team_initials, Team.all, :id, :team_initials
Should be:
f.collection_select :team_id, Team.all, :id, :team_initials
I'm looking here for my info on collection_select since I don't know it off the top of my head:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
I think this will change this parameter from "teams"=>{"team_initials"=>"27"}}, to "team_id"=>"27",
I hope that fixes it.

Accessing attributes from different associated models rails 3

I am looking to get a better understanding of Active Model/Record relationships and how to call attributes dependent upon where the attributes are located (models) and where I am calling them. So for example I can access the attribute dish_name from within the recipe controller like so
def all_recipes
#recipes = Recipe.all
end
In the view
<% #recipes.each do |r| %>
<%= r.dish_name %>
<% end %>
Now say i want to access a recipe attribute from within my controller called worldrecipes and i have just written a method that returns all recipes with the same country. a country has many recipes as a relation
So my method is
def self.top_countries
joins(:recipes).
select('countries.*, count(*) AS recipes_count').
group('countries.id').
order('recipes_count DESC')
end
My controller
#worldrecipes = Country.where(:name => params[:name])
and view
<% #worldrecipes.each do |r| %>
<%= r.name %>
<% end %>
so accessing the country name attribute is easy as its in the country model and thats where my query results are being returned from (I think)...My question is how do i access the dish_name attribute from my recipe model to that links to the country name
Hope that makes sense, does anyone have a guide on how to work this out or some golden rules for this
Thank you
I think what you need is:
#country=Country.where(:name=>params[:name]).first
#worldrecipes=#country.recipes
And in the view:
<% #worldrecipes.each do |r| %>
<%= r.dish_name %>
<% end %>
This would print the dish names of the recipes of the country with name provided by params[:name]
EDIT:
Ok Let me clear this up for you :)
Your model relationship is setup such that each country has many recipes. i.e a country has many recipes.
So you have,
has_many :recipes
in country.rb and
belongs_to :country
in recipe.rb
Now when you want to access all the recipes belonging to a country, what you do is, you call country_record.recipes (country_record being the object of the country record you need).
And when you call,
Country.where(:name=>params[:name])
What you actually get is the active record object representing the COUNTRY itself and not the recipes of the country and that is why Italy was printed.
Hope this helped you.
For starters you want to make sure you have the association setup in your models:
country.rb
class Country < ActiveRecord::Base
has_many :recipes
end
recipe.rb
class Recipe < ActiveRecord::Base
belongs_to :country
end
If you haven't already done so, add a foreign_key attribute to your recipe model by running the following migration:
rails g migration add_country_id_to_recipe country_id:integer
Now that your associations are in place you can easily query for a countries respective recipes. In your controller:
#worldrecipes = Country.where(:name => params[:name])
Then in your view:
<% #worldrecipes.each do |c| %>
<% c.recipes.each do |r| %>
<%= r.dish_name %>
<% end %>
<% end %>
In regards to 'golden rules' I highly recommend you check out Association Basics. This is the go-to place for an overview of what you can do with associations.

Rails 3: trying to follow railscast #17 to add genres to my posts

I've followed along exactly like the Railscast describes but using "genre" instead of "category," but whenever I make a post the genre_id shows as NULL and I'm getting a WARNING: can't mass assign attribute genre_ids
I only had genre_id in my post model attr_accessible because that's the name of the attribute, however when I change it to genre_ids. I get the error
ActiveRecord::StatementInvalid in PostsController#create Could not find table 'genres_posts'
post form
<% for genre in Genre.find(:all) %>
<div>
<%= check_box_tag "post[genre_ids][]", genre.id, #post.genre.include?(genre) %>
<%= genre.name %>
</div>
<% end %>
post model
class Post < ActiveRecord::Base
has_and_belongs_to_many :genres
end
I don't have an answer, but here are the things you should check:
In your code, the line <%= check_box_tag "post[genre_ids][]", genre.id, #post.genre.include?(genre) %> should include genres, not genre:
<%= check_box_tag "post[genre_ids][]", genre.id, #post.genres.include?(genre) %>
What Ryan Bates (I love his webcast) doesn't tell is that you need changes on your database so that your models may be stored. The reason for that is, that on a relational database, a m:n relation is normally realized by a separate table that just stores that relation only. Have a look at the "Rails Guides: Has and belongs to many associations".
So you have to generate a standalone migration (see the Rails Guide for that) that creates that table that hold both ids to posts and genres in one table. The table should be named therefore genres_posts, and include the columns genre_id and post_id.
So depending on what you have done before, you may follow all the steps to reach a working example application. Good luck!

How to implement multiple selection on Rails 3

I have an index where I'm showing a list of documents. I would like to implement a multiple select in order to do different actions to the documents the user has selected
I have created a
<%= check_box_tag 'id', 'document.id %>
for each document, inside a form_tag
But if I select multiple checkboxes, the params that are passed to the action are overwrited and I'm just receiving the id of the last checkbox I've selected in the id param.
¿Anyone knows how to implement multiple select?¿Any other approach?
I'm running Rails 3 and Ruby 1.8.7
Thanks in advance
You need to set :multiple => true
<%= check_box_tag 'id', document.id, :multitple => true %>
This will give you results in form of an array in params[:id]
Minor correction (plural):
<%= check_box_tag 'ids[]', document.id %>
ensure your model is properly set for attr_accessible something like :document_ids