First, I'd like to mention I'm COMPLETELY new to Ruby and Rails, I'm on my very first days of learning, so I apologize if I seem a bit unclear or too broad with my questions.
I'm trying to do something simple (I think?), which is to pivot a table.
I have a table that looks like this:
----------------------------------
| Name | Product ID | Amount |
|----------|----------------------
| Robert | P1 | 2 |
| Michael | P2 | 1 |
| Leonard | P2 | 1 |
| Robert | P2 | 4 |
| Robert | P3 | 2 |
| Michael | P3 | 1 |
----------------------------------
... and I'd like to to turn it into something like this:
---------------------------
| Name | P1 | P2 | P3 |
---------------------------
| Robert | 2 | 4 | 2 |
| Michael | - | 1 | 1 |
| Leonard | - | 1 | - |
---------------------------
I'm not too sure how to achieve that. I've looked around and haven't found anything specific to my question.
I found a gem called pivot_table, which can be found here: https://github.com/edjames/pivot_table but I have no clue how to exactly use it. It has a small guide in it, but I don't know where to place the code.
Any help is greatly appreciated.
Thank you.
Looking at your table and the results you're looking for, I would do it like this ( I assume it's an orders table ?)
result = []
Order.all.group_by(&:name).each do |name, orders|
record = {}
record["name"] = name
orders.each do |order|
record[order.product_id] = order.amount
end
result.append(record)
end
I hope this will give you a good starting point !
First install the gem
# In your Gemfile
gem 'pivot_table'
Then in your terminal, run
bundle install
Say the model represented by your first table is Sale.
sales = Sale.all
grid = PivotTable::Grid.new do |g|
g.source_data = sales
g.column_name = :product_id
g.row_name = :name
end
Then you can use the other methods listed in the docs. For example
g.column_headers # ['P1', 'P2', 'P3']
Note: this is just from reading the GitHub page you linked. I've never used the gem.
Edit:
You can put the code in a module:
# lib/PivotTable
module PivotTable
def grid(data, options = {})
grid = PivotTable::Grid.new do |g|
g.source_data = data
g.column_name = options[:column_name]
g.row_name = options[:row_name]
end
end
end
Then you'd call it from somewhere else with
include PivotTable
def my_method
sales = Sale.all
grid = grid(sales, { :row_name => :name, :column_name => :product_id })
# do stuff
end
This way you can reuse the grid-generating code and call it with arbitrary parameters.
This is a general solution, that I implemented in application_helper.rb
def pivot_table row_model, column_model, pivot_model, pivot_attribute
row_model.all.map do |r|
column_model.select(:id).sort.map do |c|
pivot_model.find_by(
row_model.to_s.downcase => r,
column_model.to_s.downcase => c
)&.public_send(pivot_attribute) || 0
end
end
end
The first three parameters are capital-letter ActiveRecord subclass names, and the pivot_attribute (which is an attribute of pivot_model) can be given as a symbol or a string. This assumes pivot_model is a many-to-many relation that references row_model and column_model as foreign keys. If there isn't a many-many model, I'm guessing some models could be repeated among the parameters, but I haven't kitchen tested it for that.
The return value is an array of arrays.
Related
I have 2 tables for example:
user_places
----------------
| id | place_id|
----------------
| 1 | 1 |
----------------
| 1 | 5 |
----------------
| 1 | 6 |
----------------
| 2 | 8 |
And a places table
--------------------------------------------
| id | title | description | image_url |
--------------------------------------------
| 1 | 1 | description1 | image1 |
--------------------------------------------
| 2 | 5 | description2 | image2 |
--------------------------------------------
| 3 | 6 | description3 | image3 |
--------------------------------------------
| ...| ... | description4 | image4 |
How to make the association in both models so I can get all places of user_places's id = 1 in rails console?
You'll want to look up foreign_keys, especially pertaining to Rails.
Rails is basically a way for you to interact with a relational database. As such, if you know how to correctly structure a relational DB, you'll be able to better understand Rails' ActiveRecord associations, and how they fit into applications.
--
How to make the association in both models so I can get all places of user_places's id = 1
You'd do this:
#app/models/user.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :places
end
#app/models/places.rb
class Place < ActiveRecord::Base
has_and_belongs_to_many :users
end
This means you have to change your user_places table to the following:
#places_users
#user_id | place_id
This will allow you to call:
$ #user = User.find "1"
$ #user.places #-> 1,5,6
--
The other answer recommended a has_many :through relationship. Whilst this allows you to add other data into your join model, it means you have to include a user_places model for no real reason (at this stage).
I would recommend using has_and_belongs_to_many for the moment. This limits you to only having references in your join table, but makes the entire association much simpler:
The big caveat here is that you'll need to change your user_places table to have an alphabetical nameflow (places_users), and make sure you only have the two foreign_keys as columns: user_id | place_id
Restaurant Load (1.5ms) SELECT * FROM "restaurants" INNER JOIN
"restaurant_branches" ON "restaurant_branches"."restaurant_id" =
"restaurants"."restaurant_id"
+----------+---------+---------+---------+----------+---------+----------+---------+---------+---------+---------+---------+---------+---------+---------+-------+
| resta... | res_... | res_... | crea... | updat... | user_id | resta... | addr...
| addr... | addr... | addr... | addr... | addr... | numb... | numb... | email |
+----------+---------+---------+---------+----------+---------+----------+---------+---------+---------+---------+---------+---------+---------+---------+-------+
| 27 | DOGG... | WE S... | 2014... | 2014-... | 4 | 28 | 405 ...
| | CHICAGO | IL | 60666 | USA | | | |
| 27 | DOGG... | WE S... | 2014... | 2014-... | 4 | 29 | 111 ...
| | CHICAGO | IL | 60661 | USA | | | |
+----------+---------+---------+---------+----------+---------+----------+---------+---------+---------+---------+---------+---------+---------+---------+-------+
As you can see, I have two records for record 27. This is from a joined
table between restaurants and restaurant_branches. How would I approach
this in a view so that when I select a record on my index.html.erb file
when it gets routed to the show.html.erb file it'll only take one row of
that record and only show me one branch instead of 2?
Thank you for any help.
The two records are different, so keep them separate and list both. It's difficult to say which fields are different from the question. Based on the query, I'm assuming they are restaurant_branches fields that are different.
If it's the restaurants that you want to show/edit then use just Restaurant.all however you want to limit. If however you want to edit the restaurant_branches then introduce a RestaurantBranchesController and have a link for the branches separate.
Something like (in erb):
<%= restaurant.id %>
<% restaurant.restaurant_branches.each do |rb| %>
<%= link_to 'Some branch', rb %><br />
<% end -%>
Then the link_to(rb) should link you to RestaurantBranchesController#show action for the particular restaurant_branch.
Collection
Further to what #vee was suggesting, it seems to me that you're calling a collection of data from ActiveRecord. A collection basically means you're going to receive a multitude of objects back, rather than just one record
I'd imagine you have your models set up as follows:
#app/models/restaurant.rb
Class Restaurant < ActiveRecord::Base
has_many :branches, class_name: "RestaurantBranch"
end
#app/models/restaurant_branch.rb
Class RestaurantBranch < ActiveRecord::Base
belongs_to :restaurant
end
This will give you the ability to call the following:
#restaurant = Restaurant.find params[:id]
#restaurant.branches #-> returns the collection
--
Limit
The way to fix this will depend on what you're trying to show.
Typically, you can use something like limit to retrieve a single record, like so:
#restaurant.branches.limit(1)
Like most things in life, this will be much simpler to resolve if you define what you want to limit the results for - what's the purpose?
I need to fetch like the winner bids, and a bid can be for a different date (don't ask why), so I need to select the bid with minimum bid price for each day.
Here are my models
Leilao
has_many :bids
Bid
belongs_to :leilao
#winner_name
#price
#date
I tried a solution already and got close to what I need. The problem is that, in some cases, when I create a new bid with lower price, I don't know why the results do not change.
Leilao.find(1).bids.having('MIN("bids"."price")').group('date').all
This seems to work, but as I said, it does not work in some cases when I create a new bid. But it worked properly once. So, if you do know what might be happening, please tell me.
I then searched for some way for doing this and I got the following
Leilao.find(1).bids.minimum(:price, :group => :date)
which works properly, but with this, I just fetch the dates and prices and I need all the bid data.
I could get it by doing this, but it feels really bad to me
winner_bids = Leilao.find(1).bids.minimum(:price, :group => :date)
winners_data = []
winner_bids.each do |date, price|
winners_data << Leilao.find(1).bids.where(price: price, date: date).first
end
winners_data
Any idea a better way to do this? Or what's wrong with my first approach?
Performance is not an issue, since this is just for academic propose but it just feels nasty for me
Also those Leilao.find(1) is just for explaining it here, I'm not using it allover the place, no.
Thanks in advance
see this
mysql> select * from bids;
+----+-------------+-------+------------+-----------+
| id | winner_name | price | date | leilao_id |
+----+-------------+-------+------------+-----------+
| 1 | A | 1.1 | 2012-06-01 | 1 |
| 2 | A | 2.2 | 2012-06-01 | 1 |
| 3 | A | 3.3 | 2012-05-31 | 1 |
| 4 | A | 4.4 | 2012-05-31 | 1 |
+----+-------------+-------+------------+-----------+
4 rows in set (0.00 sec)
mysql> select * from bids where leilao_id = 1 group by date order by price asc;
+----+-------------+-------+------------+-----------+
| id | winner_name | price | date | leilao_id |
+----+-------------+-------+------------+-----------+
| 1 | A | 1.1 | 2012-06-01 | 1 |
| 3 | A | 3.3 | 2012-05-31 | 1 |
+----+-------------+-------+------------+-----------+
2 rows in set (0.00 sec)
in rails
1.9.2-p290 :013 > Leilao.find(1).bids.order(:price).group(:date).all
[
[0] #<Bid:0x00000003553838> {
:id => 1,
:winner_name => "A",
:price => 1.1,
:date => Fri, 01 Jun 2012,
:leilao_id => 1
},
[1] #<Bid:0x00000003553518> {
:id => 3,
:winner_name => "A",
:price => 3.3,
:date => Thu, 31 May 2012,
:leilao_id => 1
}
]
As Amol said its not going to work, the SQL looks like
SELECT "whatever".* FROM "whatever" GROUP BY date ORDER BY price
But the group_by will be applied before order_by
I had this trouble when i had market table where is stored different types of records, i solved it by writing a class method, maybe not the best approach but working
def self.zobraz_trh(user)
markets = []
area = self.group(:area).all.map &:area
area.each do |market|
markets << self.where(["area = ? and user_id != ?", market,user]).order(:price).first
end
markets
end
where self is Market class
I am new to coding (never taken a CS class and have a very very minimalist understanding of the MVC architecture) and working on a test project to learn it :-) I want to setup some tables tables:
Table 1:
Table_1 ID | Name | Attribute_1 | Attribute_2 | Attribute_3 | etc
1 | Blah | attribute 1 | attribute 2 | attribute 3 |
Table 2:
Table_2 ID | Skill |
1 | nun_chucks |
2 | bow |
3 | arrow |
Table 3:
Table_3 ID | Primary_1 ID | Primary_2 ID |
1 | 1 | 1 |
2 | 1 | 2 |
3 | 1 | 3 |
And then be able to visualize the data so it reads like:
Name | nunchucks, bow, arrow | attribute 1 | attribute 2 | attribute 3 |
I have used Scaffold to create the MVC for Table 1 & 2 and have the #page for each one respectively. I am not sure how to combine them to create table 3 and then make the data show as I am asking.
Thanks!
This is a good reference for what you're doing: http://guides.rubyonrails.org/association_basics.html
INNER JOIN is what you want which is a database operation.
Take a look in your database servers manual and read up on it.
Essentially it takes two tables, joins them by a given column (for instance, Table_2.ID and Table_3.ID) and spits out a summary of both tables joined leaving out columns that don't match across the tables.
Given your example,
and the following contoller in Rails 3
class SomeController < ActionController
def show
#table_1 = Table1.find(params[:id],:include => :table2)
# Table1 and Table2 are your models
# where table2 would be your has_many_and_belongs_to_many assoc in Table1
end
end
you could write this up in show.html.erb like
<%= #table_1.name %>
<%= #table_1.table_2.map(&:skill) %>
<%= #table_1.attribute_1 %>
<%= #table_1.attribute_2 %>
<%= #table_1.attribute_3 %>
I don't understand how to get the columns I want from rails. I have two models - A User and a Profile. A User :has_many Profile (because users can revert back to an earlier version of their profile):
> DESCRIBE users;
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(255) | NO | UNI | NULL | |
| password | varchar(255) | NO | | NULL | |
| last_login | datetime | YES | | NULL | |
+----------------+--------------+------+-----+---------+----------------+
> DESCRIBE profiles;
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
| first_name | varchar(255) | NO | | NULL | |
| last_name | varchar(255) | NO | | NULL | |
| . . . . . . |
| . . . . . . |
| . . . . . . |
+----------------+--------------+------+-----+---------+----------------+
In SQL, I can run the query:
> SELECT * FROM profiles JOIN users ON profiles.user_id = users.id LIMIT 1;
+----+-----------+----------+---------------------+---------+---------------+-----+
| id | username | password | last_login | user_id | first_name | ... |
+----+-----------+----------+---------------------+---------+---------------+-----+
| 1 | john | ****** | 2010-12-30 18:04:28 | 1 | John | ... |
+----+-----------+----------+---------------------+---------+---------------+-----+
See how I get all the columns for BOTH tables JOINED together? However, when I run this same query in Rails, I don't get all the columns I want - I only get those from Profile:
# in rails console
>> p = Profile.joins(:user).limit(1)
>> [#<Profile ...>]
>> p.first_name
>> NoMethodError: undefined method `first_name' for #<ActiveRecord::Relation:0x102b521d0> from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.1/lib/active_record/relation.rb:373:in `method_missing' from (irb):8
# I do NOT want to do this (AKA I do NOT want to use "includes")
>> p.user
>> NoMethodError: undefined method `user' for #<ActiveRecord::Relation:0x102b521d0> from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.1/lib/active_record/relation.rb:373:in method_missing' from (irb):9
I want to (efficiently) return an object that has all the properties of Profile and User together. I don't want to :include the user because it doesn't make sense. The user should always be part of the most recent profile as if they were fields within the Profile model. How do I accomplish this?
I think the problem has something to do with the fact that the Profile model doesn't have attributes for User...
Use select() to name the columns you want. At least this works in Rails 3.0.9.
Background: my application has a primary table named :rights. I wanted to be able to ascribe a tag and color to a given :right record so I could easily pick it out of an index listing. This doesn't cleanly fit the Rails picture of associated records; most :rights will never be tagged, and the tags are completely arbitrary (user input via tag/edit).
I could try duplicating the tag data in the :right record, but that violates normal form. Or I could try querying :tags for each :right record, but that is a painfully inefficient approach. I want to be able to join the tables.
MySQL console shows:
mysql> describe rights;
+------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
...
| Tagid | int(11) | YES | | NULL | |
+------------+---------------+------+-----+---------+----------------+
mysql> describe tags;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| TagName | varchar(255) | YES | | NULL | |
| TagColor | varchar(255) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
I am going to use TagName and TagColor in views/rights/index.html.erb, so I want the rights controller to include those columns in the #rights object it passes to the view. Since not every :right has a :tag, I want to use an outer join:
#rights = Right.joins("LEFT OUTER JOIN tags ON rights.Tagid = tags.id")
But, as everyone has found, this alone doesn't work: a block reference to TagName produces a server error. However, if I add a select at the end, all is well:
#rights = Right.joins("LEFT OUTER JOIN tags ON rights.Tagid = tags.id").select("rights.*,tags.TagName as TagName,tags.TagColor as TagColor")
Note added 6/7/13: the select clause does not require aliases - this works too:
.select("rights.*,tags.TagName,tags.TagColor")
Now I can reference TagName and TagColor in my view:
<% #rights.each do |right| %>
<tr ALIGN=Left <%=
# color background if this is tagged
" BGCOLOR=#{right.TagColor}" if right.TagColor
%> > ...
<% end %>
I don't think that you can load users and profiles with join in Rails. I think that in earlier versions of Rails ( < 2.1) loading of associated models was done with joins, but it was not efficient. Here you have some explanation and links to other materials.
So even if you explicite say that you want to join it, Rails won't map it to associated models. So if you say Profile.whatever_here it will always be mapped to Profile object.
If you still want to do what you said in question, then you can call custom sql query and process results by yourself:
p = ActiveRecord::Base.connection.execute("SELECT * FROM profiles JOIN users ON profiles.user_id = users.id LIMIT 1")
and get results row by row with:
p.fetch_row
It will already be mappet to an array.
Your errors are because you are calling first_name and user method on AciveRecord::Relation object and it stores an array of Profile objects, not a single object. So
p = Profile.joins(:user).limit(1)
p[0].first_name
shoud work.
Better way to fetch only one record is to call:
p = Profile.joins(:user).first
p.first_name
p.user
But when you call p.user it will query database. To avoid it, you can use include, but if you load only one profile object, it is useless. It will make a difference if you load many profiles at a time and want to inlcude users table.
Try using select("*").joins(:table)
In this case, you would type:
User.select("*").joins(:profile)
Hope that works for you.
After reading these tips I got the joins to all be loaded in one query by reading 3 ways to do eager loading (preloading) in Rails 3 & 4.
I'm using Rails 4 and this worked like a charm for me:
refs = Referral.joins(:job)
.joins(:referee)
.joins(:referrer)
.where("jobs.poster_id= ?", user.contact_id)
.order(created_at: :desc)
.eager_load(:job, :referee, :referrer)
Here were my other attempts.
#first attempt
#refs = Referral.joins(:job)
# .where("jobs.poster_id= ?", user.contact_id)
# .select("referrals.*, jobs.*")
# works, but each column needs to be explicitly referenced to be used later.
# also there are conflicts for columns with the same name like id
#second attempt
#refs = ActiveRecord::Base.connection.exec_query("SELECT jobs.id AS job_id, jobs.*, referrals.id as referral_id, referrals.* FROM referrals INNER JOIN jobs ON job_id = referrals.job_id WHERE (jobs.poster_id=#{user.contact_id});")
# this worked OK, but returned back a funky object, plus the column name
# conflict from the previous method remains an issue.
#third attempt using a view + rails_db_views
#refs = JobReferral.where(:poster_id => user.contact_id)
# this worked well. Unfortunately I couldn't use the SQL statement from above
# instead of jobs.* I had to explicitly alias and name each column.
# Additionally it brought back a ton of duplicate data that I was putting
# into an array when really it is nice to work with ActiveRecord objects.
#eager_load
#refs = Referral.joins(:job)
# .where("jobs.poster_id= ?", user.contact_id)
# .eager_load(:job)
# this was my base attempt that worked before I added in two more joins :)
I have got round this problem by creating a VIEW in the database which is the join, and then referencing that as if it were a normal ActiveRecord table in the code. This is fine for getting data out of the database, but if you need to update it, then you'll need to go back to the base classes that represent the 'real' tables. I have found this method to be handy when doing reports that use biggish tables - you can get the data out all in one hit. I am surprised that this doesn't seem to be built into ActiveRecord, seems an obvious thing to me!
So for you:
IN SQL:
CREATE VIEW User_Profiles
AS
SELECT P.*, U.first_name
FROM Users U
inner join Profiles P on U.id=P.user_id
IN RUBY models file:
class UserProfile < ActiveRecord::Base
self.primary_key = :id
#same dependencies as profiles
end
**HINT... I always forget to set the owner of the view (I use postgres), so it blows up straight away with much cursing and self-recrimination.