lundi 13 février 2017

Associations aren't working as expected in rails 5

Vote count: 0

I'm not able to show the intended data for the following situation:

I have a User model with 2 roles: Consumers and Designers. I'm using Devise for :users.

A designer can create projects and can add multiple photos to that project. I'm using the model :photos for that.

A consumer can create designbooks and can also add multiple photos to a designbook. The same model :photos is being used by making it polymorphic.

Here's the tricky part: a consumer can also like a project-photo and connect it to his or her designbook(s). The HABTM model :designbooks_photos is being used for that.

Here's the code:

Users model

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_attached_file :avatar, styles: { medium: "300x300#", thumb: "100x100#" }, default_url: "/missing/avatar/:style.jpeg"
  validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/

  enum role: [:consumer, :designer]

  validates :name, presence: true
  validates :role, presence: true

  has_many :projects, dependent: :destroy
  has_many :designbooks, dependent: :destroy

Projects model

class Project < ApplicationRecord
  belongs_to :user
  has_many :photos, as: :photoable, dependent: :destroy
  has_one :photo, as: :photoable
  has_many :designbook_likes, through: :photos

Photos model

class Photo < ApplicationRecord
 has_attached_file :image, styles: { masonry: "360", medium: "360x203#", small: "180x101#", thumb: "40x40#" }, default_url: "/missing/projects/:style.png"
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/

  belongs_to :project
  belongs_to :photoable, polymorphic: true
  has_and_belongs_to_many :designbook_likes, class_name: "Designbook", foreign_key: "designbook_id", join_table: :designbooks_photos

Designbooks model

class Designbook < ApplicationRecord
  belongs_to :user
  has_and_belongs_to_many :photo_likes, class_name: "Photo", foreign_key: "photo_id", join_table: :designbooks_photos, dependent: :destroy
  has_many :projects, as: :designbook_like, through: :photo_likes
  has_many :photos, as: :photoable, dependent: :destroy
  has_one :photo, as: :photoable

DesignbooksPhotos model

class DesignbooksPhotos < ApplicationRecord
  belongs_to :photo_like, class_name: "Photo", foreign_key: "photo_id"
  belongs_to :designbook_like, class_name: "Designbook", foreign_key: "designbook_id"

And here is the controller-method for profiles:

class ProfilesController < ApplicationController
  before_action :authenticate_user!

  def show
    @user = current_user
    @projects = @user.projects
    @designbooks = @user.designbooks

CRUD works correctly for all the models (BDD tested with Cucumber) but when I try to show selected project-photos for a designbook, then it's not showing the expected photo's which I can see in the database.

Next to that I'm also not able to show a correct designbook-count for projects; It seems to show all connected designbooks of all the projects for just the first designbook in the array.

Code for showing designbook-count for a project:


<%= link_to "+Add a project", new_project_path %>

<%= render partial: "project", collection: @projects %>


<%= link_to project do %>
<% if %> <%= image_tag %> <% else %> Card image cap <% end %>
<% end %>

The line seems to give the wrong value here.

Code for showing selected project-photos for a designbook:


<%= link_to "+Add a designbook", new_designbook_path %>

<%= render partial: "designbook", collection: @designbooks %>


<%= link_to designbook do %>
<% if %> <%= image_tag %> <% else %> Card image cap <% end %>
<% end %>

This partial seems to work with an empty array, which should have at least 1 photo.

Can anyone help me out? I'm not sure if all associations are okay.

asked 32 secs ago

Let's block ads! (Why?)

Associations aren't working as expected in rails 5

Aucun commentaire:

Enregistrer un commentaire