Reputation: 1522
Model for Plugin:
class Plugin < ActiveRecord::Base
has_many :vulns
end
Model for Vuln:
class Vuln < ActiveRecord::Base
belongs_to :plugin
end
Table for Plugin:
create_table "plugins", force: true do |t|
t.string "name"
end
Table for Vuln:
create_table "vulns", force: true do |t|
t.string "title"
t.integer "plugin_id"
t.string "vulnerability_type"
t.string "fixed_in"
end
When I use rails console using rails c
and enter Plugin.select("*").joins(:vulns)
it only grabs data from the Plugin table
#<ActiveRecord::Relation [#<Plugin id: 1, name: "xxx">, #<Plugin id: 2, name: "xxx">
This is the query:
SELECT * FROM `plugins` INNER JOIN `vulns` ON `vulns`.`plugin_id` = `plugins`.`id`
However, when I execute this query in mysql, it shows all content from vulns and plugins like its supposed to. For every plugin there is atleast one or more vuln in the database.
So here's the question: How do I get it to fetch content from both tables (plugins and vulns) instead of just plugins?
Upvotes: 0
Views: 824
Reputation: 38645
The vulns
values are there, it's just not shown because you are using Plugin
model to select, i.e. Plugin.select("*").joins(:vulns)
.
If you did the following, you will get the value:
> plugin = Plugin.select("*").joins(:vulns)
> plugin.first.title
=> "mytitle"
Because you are querying through Plugin
model, you'll see Plugin
object.
Another way to test this would be by doing the following:
> plugin = Plugin.select([:name, :title]).joins(:vulns)
=> #<ActiveRecord::Relation [#<Plugin id: 1, name: "xxxx">]>
# It won't show you title even though it's there
> plugin.title
=> "mytitle"
Upvotes: 1