mysql - Rails 4 .joins() issue -- not working -
model plugin:
class plugin < activerecord::base has_many :vulns end
model vuln:
class vuln < activerecord::base belongs_to :plugin end
table plugin:
create_table "plugins", force: true |t| t.string "name" end
table vuln:
create_table "vulns", force: true |t| t.string "title" t.integer "plugin_id" t.string "vulnerability_type" t.string "fixed_in" end
when use rails console using rails c
, enter plugin.select("*").joins(:vulns)
grabs data plugin table
#<activerecord::relation [#<plugin id: 1, name: "xxx">, #<plugin id: 2, name: "xxx">
this query:
select * `plugins` inner join `vulns` on `vulns`.`plugin_id` = `plugins`.`id`
however, when execute query in mysql, shows content vulns , plugins supposed to. every plugin there atleast 1 or more vuln in database.
so here's question: how fetch content both tables (plugins , vulns) instead of plugins?
the vulns
values there, it's not shown because using plugin
model select, i.e. plugin.select("*").joins(:vulns)
.
if did following, value:
> plugin = plugin.select("*").joins(:vulns) > plugin.first.title => "mytitle"
because querying through plugin
model, you'll see plugin
object.
another way test doing following:
> plugin = plugin.select([:name, :title]).joins(:vulns) => #<activerecord::relation [#<plugin id: 1, name: "xxxx">]> # won't show title though it's there > plugin.title => "mytitle"
Comments
Post a Comment