0

I don't know my heading title good or bed because I'm new in ruby on rails, I'm troubling one conditional issue like below example.

I have three table like user, post & saved_post

user table

user_id | user_name | --------------------- 1 | ABC | --------------------- 2 | efg | 

post table

 post_id | title | -------------------- 1 | XYZ | -------------------- 2 | xyz | 

saved_post table

 id | user_id | post_id | ----------------------------- 1 | 1 | 2 | 

View

<% @post.each do |p| %> <%= p.post_title %> <%= form_for :create, url: home_path(@save), action: :create, method: :post do |f| %> <%= f.hidden_field :post_id, :value => p.post_id %> <button class="btn btn-default btn-save" type="submit">Save</button> <% end %> <% end %> 

If user_id 1 save post_id 2 in the saved_post table then show this post saved only for user_id 1, otherwise show save.

How can I reach this solution?

5
  • Is one post related only to one user or several users? Commented Mar 10, 2016 at 12:48
  • several users like user_id 1 save post_id 1 & also user_id 2 save post_id 1 such as & without login he just see Commented Mar 10, 2016 at 12:51
  • Do you have any current_user kind of variable to access the user information? Commented Mar 10, 2016 at 14:02
  • Yes i have it current_user Commented Mar 10, 2016 at 14:18
  • what is '@save'? can you show the relevant controller action methods? Commented Mar 10, 2016 at 14:35

2 Answers 2

1

You can try has_many :through Association which Active Record have. Refer this link.

class Users < ActiveRecord::Base has_many :posts has_many :saved_posts, through: :posts end class Post < ActiveRecord::Base belongs_to :user belongs_to :saved_post end class SavedPost < ActiveRecord::Base has_many :posts has_many :users, through: :posts end 

More details regarding this you will find in above link.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm trying to just showing conditionally, saving to database is ok
0

Rails is all about convention over configuration. Meaning if you write your Rails applications following Rails conventions, you'll have huge benefit from it.

Your tables should be named in plural form. Tables and Posts. What I understood from your comments is that user has many posts and posts can belong to many users. This is typical has_and_belongs_to_many association.

If you rename your saved_post table as posts_users (both table names in alphabetical order) Rails will know how to deal with these tables automatically.

Database should look like this:

enter image description here

I made a sample application with your two user and methods to add posts to either of those users.

 # routes.rb root 'posts#index' resources :posts # posts_controller.rb class PostsController < ApplicationController before_action :set_user def index @posts = @current_user.posts end def new @post = @current_user.posts.build(title: 'totally new title') end def edit @post = Post.find(params[:id]) end def create @post = Post.new(post_params) @post.save redirect_to posts_path end def update @post = Post.find(params[:id]) if @post.update(post_params) redirect_to posts_path else render 'edit' end end # -------------------------------------------------------------------------- private # -------------------------------------------------------------------------- # Sets the user based on the params value def set_user if params[:user_id] @current_user = User.find(params[:user_id]) else # If not found, sets the first from the Users table @current_user = User.first end end def post_params params.require(:post).permit(:title, {user_ids: []}) end end 

index

enter image description here

# index.html.erb <h3>Users</h3> <% User.all.each do |user| %> <%= link_to user.username, posts_path(user_id: user.id) %> <br /> <% end %> <h3>Posts for the user: <%= @current_user.username %></h3> <p> <% @posts.each do |post| %> <%= link_to post.title, edit_post_path(post) %> <br /> <% end %> </p> <p> <%= link_to 'Create new post', new_post_path %> </p> 

In edit you can choose which users are attached to this post:

enter image description here

# edit.html.erb <%= render :partial => "post_form", locals: {button_name: 'update'} %> # _post_form.html.erb <h3>Post <%= @post.title %> for the user <%= @current_user.username %></h3> <%= form_for @post do |f| %> <%= f.text_field :title %> <br /> <p> <label> <%= f.collection_check_boxes(:user_ids, User.all, :id, :username) %> <label><br /> </p> <p> <%= f.submit button_name, class: "btn btn-default btn-save" %> </p> <% end %> 

In post.rb and user.rb files you need to specify association between these two classes. And when you have posts_users table named correctly, Rails finds it automatically.

# post.rb class Post < ActiveRecord::Base has_and_belongs_to_many :users end # user.rb class User < ActiveRecord::Base has_and_belongs_to_many :posts end 

And if you create a new title, if will use the same form:

enter image description here

# new.html.erb <%= render :partial => "post_form", locals: {button_name: 'create'} %> 

Comments