Andre Zimpel

Joined

1,020 Experience
8 Lessons Completed
0 Questions Solved

Activity

Posted in Code Review: Run Number Refactoring Discussion

Awesome episode dude!

I still got one question. Let's say I want to add this logic to an existing model with some existing entries in the database. How would I update them when I deploy the app?

Posted in How do I order by an associations serialised value?

Folowing setup:

I got a rails app with dynamic content types. An user can create a content type and add fields (string, integer...) to it. Those are basically the smae like reugale rails models but they are managable through the app and the ui.

So I got:

class ContentType < ApplicationRecord
  has_many :content_type_fields
  has_many :content_entries
  has_many :content_entry_fields, through: :content_entries
end
class ContentTypeField < ApplicationRecord
  belongs_to :content_type
  has_many :content_entry_fields
  has_many :content_entries, through: :content_entry_fields
end

In addition to that I got content entries. The content entry belongs to an content type that defines the available fields. Plus there are content entry fields. They represent the content for a field for an entry.

class ContentEntry < ApplicationRecord
  belongs_to :content_type
  delegate :content_type_fields, to: :content_type
  has_many :content_entry_fields
end
class ContentEntryField < ApplicationRecord
  belongs_to :content_entry
  belongs_to :content_type_field

  serialize :value, JSON
end

I do serialize the value of the content entry field to enable localisation, e.g.: {"en-US":true,"de-DE":false}.

An example:

There is a content type called billboards. It has a content type field for title, link and link_color.
Based on the type let's say there are the following entries:

1, Shop, shop.example.com, black
2, Lookbook, lookbook.example.com, #eee
3, About Us, example.com/about-us, red.

Please keep in that the values in the database would be something like {"en-US":"About Us","de-DE":"รœber uns"}.

Well, here comes the question:

How do I order all the entries based on the content entry field?

Something like: order all entries by title ASC or by link DESC.

I hope you get what I want to achieve. Basically I want to be able to dynamically create models through the ap ui, add entries based on that and then serve them through an api.

Thank you very much!

Posted in Our First API (Example) - GoRails

Can't wait for the webhooks ๐Ÿ™Š

Posted in Update and validate multiple entries

Well, there is one thing I wish I could handle differently: what if 3 of the actions fail due a validation error? With the transaction every error would be validated step by step and not all errors as a whole ๐Ÿค”

Posted in Update and validate multiple entries

Thank you very much! Transactions work great!

I just hat do change

content_entry_field.update_attributes(value: params[:entryData][:fields][api_field.to_sym])

to

content_entry_field.value = params[:entryData][:fields][api_field.to_sym]
content_entry_field.save!

in order to raise an exception if there is an validation error.

Posted in Update and validate multiple entries

Hi friends

I got a kinda tricky problem which I usually would do through a nested form, but since I'm writing an API which has some tricky associations I need something custom (I guess):

First of all I got a ContentModel. A ContentModel has many ContentModelFields. They just describe of what kinds of fields a ContentModel consists, e.g. title:string, year:integer..., pretty much the same as a rails model but it can be created and edited dynamically via an backend.

In addition to that I got a ContentEntry. That one has got a relation to the ContentModel so that's clear what kind of model the ContentEntry describes. Also a ContentEntry has got many ContentEntryFields. They store the ContentEntry id plus an ContentModelField id and a value.

An Example:

ContentModel:
id: 1
title: Lookbook
ContentModelfields
id: 1
content_model_id: 1
title: Title
field_id: title
field_type: string

id: 2
content_model_id: 1
title: Year
field_id: year
field_type: integer

id: 3
content_model_id: 1
title: Link
field_id: link
field_type: string
An then there is a ContentEntry
id: 1
content_model_id: 1

ContentEntryFields
id: 1
content_model_field_id: 1
content_entry_id: 1
value: PRACTICE

id: 2
content_model_field_id: 2
content_entry_id: 1
value: 2016

id: 3
content_model_field_id: 3
content_entry_id: 1
value: /drops/practice

Now... there is the tricky part: I want to write an API which updates the entries. I'd like the PUT request (/api/v1/entries/1)to send some fields like so:

fields: {
  title: PRACTICE,
    year: 2016,
    link: /drops/practice
}

or

fields: {
  title: OCEANS,
    year: 2017,
    link: /drops/oceans
}

As far as I (and google) know, I can't use the nested stuff with accepts_nested_attributes_for here since I first of all need to find the ContentModelFields in order to know which ContentEntryField is the right one for title, year or link. Than I need to find that exact ContentEntryField based on the field_id of the ContentModelField and update that with the new value.

So far so good. I've done that whole thing this way (@content_entry get's set by an before_action):

# get fields from params
api_fields = params[:entryData][:fields]

# find or create on  all (!) fields with the new values
api_fields.each do |api_field|
  content_model_field = @content_entry.content_model_fields.where(field_id: api_field).first
  content_entry_field = @content_entry.content_entry_fields.where(content_model_field_id: content_model_field.id).first_or_create

  content_entry_field.update_attributes(value: params[:entryData][:fields][api_field.to_sym])
end

Now on to the problem: I want to add validations (which are stored within the ContentModelField). Currently if the ContentModel consists of three fields and there might be an validation error (or any other error) on the third one, the first two would be saved which would be not so cool since I want to update the ContentEntry as a whole.

Now I don't know what the f to do :/

Thank you for your help!

Posted in How to send Webhook?

Thank you for your reply!

Ouuuhhh that would be so nice!

One last question: I'd like to keep track of the webhooks. Like a separate site that displays all past (and active) hooks which are saved in a database with a fail count and maybe a response. How would I keep track of the response (just talking about fail or success)?

Posted in How to send Webhook?

I'm building a content management service which needs to send a webhook everytime the user saves a record.

What's a best practice to send webhooks in a rails app? A LOT of tutorials are about receiving them but none of them covers sending one. I hope somebody can help me out!

Thank you!

Posted in Group Chat with ActionCable: Part 2 Discussion

Thank you for your fast reply! Idk why I got an error first that i should remove the existing indices. It works now tho. Thank you very much!

Posted in Group Chat with ActionCable: Part 2 Discussion

@excid3:disqus awesome tutorial series!

You've said around 6:15 it would be good to add indices to the chatroom_users table. I just can't figure out of to add an uniq index for the chatroom and user references. Do you got a tip for that?

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.