I'm used to MySQL but trying to use Ruby on Rails right now. In MySQL, I would have two tables, with one containing a reference to another ("posts" referring to "topic"). A MySQL query doing what I want would be similar to "SELECT * FROM Posts WHERE posts.topic="topic" ("topic" here is a variable).
However, trying to work with the Ruby model stuff has me confused. The variables being passed between the controller and view are null because they are empty tables.
In my controller:
def topic @topic = Topic.where(params[:topic]) @posts = Post.where(topic: @topic.object_id) end I don't know how to select the posts which have the topic defined by the "topic" variable.
In the view:
<% @posts.each do |post| %> <p><%= post.title %></p> <% end %> The migration files:
class CreatePosts < ActiveRecord::Migration def change create_table :posts do |t| t.string :title t.string :text t.references :topic t.timestamps end end end class CreateTopics < ActiveRecord::Migration def change create_table :topics do |t| t.string :topic t.timestamps end end end