Belongs to
# avo-2
b
I am having a problem finding how to submit a model where the model belongs_to:user.
l
What do you mean? Was is there failing? How are you submitting the form?
b
maybe I just don't understand how it works.
In the initializer, I uncommented:
`` ## == Authentication == config.current_user_method = :current_user config.authenticate_with do end``
and now the user's first and last name appears in the lower left
l
That’s good!
b
I try to save and I get this :
l
Oh. Ok. Now I understand
So, that model requires to have user_id to it, right
You can do a few things. Add ‘field :user, as: :belongs_to’ to that resource
b
right:
``class BlogArticle < ApplicationRecord validates :title, presence: true # extend FriendlyId # friendly_id :title, use: :slugged WORDS_PER_MINUTE = 150 belongs_to :user has_rich_text :content has_one_attached :cover_photo ########################## # => Methods ########################## def reading_time # https://alexanderpaterson.com/posts/showing-estimated-reading-time-on-a-rails-blog-post text = self.body.to_plain_text result = (text.scan(/\w+/).length / WORDS_PER_MINUTE).to_i ||= 1 end end ``
l
Pfff. Reading code is so difficult here
Have you seen the demo video on the homepage?
b
anyway, when I generated the model, I wrote ``user:belongs_to``
l
I think I demo the belongs to association too there
That’s not correct IMHO
b
oh?
probably why I am having such a hard time.
from my logs: ``rails g resource blog_article user:belongs_to title``
hopefully getting back to Avo, what does ``field :user, as: :belongs_to`` do?
I don't want to take up too much of you valuable time.
l
Hello, i just checked that cmd locally and it's actually wrongly generating
field :user, as: :text
on the resource, you should have it too, right?
Using
user:references
instead
user:belongs_to
on the generator works as expected, meanwhile I'm creating a issue and trying to fix that generation issue.
b
hi @loud-jewelry-99127
l
belongs_to
is one of many field types that Avo supports, it purpose is to be used on
belongs_to
associations, check https://docs.avohq.io/2.0/associations/belongs_to.html
b
looks at docs
l
Sorry. I’m AFK now, but ‘user:references’ in terminal should create the roght association. Then ‘field :user, as: :belongs_to’ should work perfecr
b
@loud-jewelry-99127 , just so I am clear, if I have a model called BlogArticle with a title and it belongs_to the user or is generated with ``references``, what is the proper way to generate from the command line?
l
For the model generation both
references
and
belongs_to
are valid commands. You did everything right. To fix the form and the resource you should make sure that the user field in
BlogArticleResource
looks similar to
field :user, as: :belongs_to
On next model generations, until we fix it, i would suggest to use
references
just to assure that Avo resource generator generates the fields correctly.
b
ok, just a sec.
I made the change and I restarted the server.
so it's requiring the user, but it's not applying the current_user
l
Restart is not required on resource changes, i suggest restart when you're working with actions
b
ok
the validation requires a user, but the current_user from devise is not being applied.
l
That's because you didn't specified that anywhere
b
so `` field :user, as: :references, default: :current_user`` ?
l
That do not work, default option should take a block
default: -> { current_user.id }
b
ah, ok
l
I mean, it's not mandatory to do it with the block, you can specify the default id statically, the block is just to be executed every time than that field is "invoked"
Since you're already assigning the current user to that field maybe you want to disable it in order to make it not editable?! Check https://docs.avohq.io/2.0/field-options.html#disabled
I suggest you to explore the Docs to see the possibilities that you have
b
OK, I probably need to come back to this later.
@loud-jewelry-99127 , thanks for all of your help.
l
Sure, anytime! Happy coding!
b
afternoon all!
Is there a way already set up to use drag and drop to sort?
on second thought, nm
l
I'll answer the question anyway 🙂
no, there isn't a drag and drop way of doing that
... yet
I created this issue. maybe anyone can pick it up before we do
we're focusing all our efforts on launching Avo 3