Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First record in a table has no slug #580

Closed
benbonnet opened this issue Jul 2, 2014 · 16 comments
Closed

First record in a table has no slug #580

benbonnet opened this issue Jul 2, 2014 · 16 comments

Comments

@benbonnet
Copy link

It's all in the title, it won't bother that much as the first record is often a test record, but felt like something was missing somewhere.

Is there something in the setup that needs to be specified, or do we need to force a default to the slug field ?
As it is now, it seems that the slug does not generate if friendly_id does not have any other slugs to be compared to

@benbonnet benbonnet changed the title Not a big deal, but first record in a table has no slug But first record in a table has no slug Jul 2, 2014
@benbonnet benbonnet changed the title But first record in a table has no slug First record in a table has no slug Jul 2, 2014
@parndt
Copy link
Collaborator

parndt commented Jul 2, 2014

What steps can you provide us to reproduce this issue please?

@benbonnet
Copy link
Author

I have a plain setup as shown in the README

extend FriendlyId
friendly_id :slug_candidates, :use => [:slugged]
def slug_candidates
  [
    :title,
    [:title, :id]
  ]
end

version is friendly_id (5.0.4)

@parndt
Copy link
Collaborator

parndt commented Jul 2, 2014

@bbnnt so you merely have to create a record like this:

YourModel.create title: 'something'

...and you see the problem?

@benbonnet
Copy link
Author

Again, I'm not accurate enough, sorry.
The title is empty when first saved, but shouldn't it add the long 'hash' (or I don't know how you call it) as slug ?
I wonder why the slug can be empty, whatever the reason is

@parndt
Copy link
Collaborator

parndt commented Jul 2, 2014

Would this solve your issues?:

extend FriendlyId
friendly_id :slug_candidates, :use => [:slugged]
def slug_candidates
  [
    :title,
    [:title, :id]
  ]
end
validates :title, :presence => true

I would have thought that FriendlyId would expect a column that can't be blank to generate slugs from, would that make sense in your application? I've never used FriendlyId with a nullable field.

@benbonnet
Copy link
Author

I guess that would sure work, as the record wouldn't be saved then, not passing the validation.

I maintain, with only one record in the table, the first one, created with the following :

s = Song.create(:some_other_field_than_title => "xxxxx")

The record get saved, slug is null. I go to the console, get back the record, update another field :

s = Song.first
s.yet_another_field_but_not_title = "some value"
s.save

slug still blank. Let's save a title :

s = Song.first
s.title = "some value"
s.save

slug still blank.

It then works for all the further records. But if I even try to have the expected behavior on the very first record, it does not change (I save this discussion from the console tests on this las statement, but the slug does not get filled)

For the very first record, the slug has to be filled manually

@parndt
Copy link
Collaborator

parndt commented Jul 2, 2014

What if you remove the slug_candidates method?

@benbonnet
Copy link
Author

Simplified to it :

extend FriendlyId
friendly_id :title

but still the same

@parndt
Copy link
Collaborator

parndt commented Jul 2, 2014

@norman @xymbol should we support this configuration whereby the sluggable field is NULLable?

@benbonnet
Copy link
Author

From what I see... I captured here the db records -> http://cl.ly/image/2N321a1d0r2P

That's fine with a default unique hash ! What I'm saying is that Friendly_id do its job for the second one and all the others records coming after the second one, but will never do it for the first one
As said in the first comment, nobody would die; but feels like something is missing

@xymbol
Copy link
Collaborator

xymbol commented Jul 2, 2014

@parndt Actually, I think this won't happen once #571 gets merged in. /cc @norman

@parndt
Copy link
Collaborator

parndt commented Jul 2, 2014

@bbnnt can you try the experimental branch?

gem 'friendly_id', :git => 'https://github.com/xymbol/friendly_id', :branch => 'fix-blank-slugs'

@benbonnet
Copy link
Author

I guess @xymbol 's branch fixes this ! -> http://cl.ly/image/1D0A0E1S2D3y

@norman
Copy link
Owner

norman commented Jul 2, 2014

@bbnnt @parndt I just merged @xymbol's branch into master; we'll release 5.1.0 soon with his changes.

@norman norman closed this as completed Jul 2, 2014
@xymbol
Copy link
Collaborator

xymbol commented Jul 2, 2014

👍

@benbonnet
Copy link
Author

thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants