I'm trying to use Friendly ID to create more custom urls. However, the column I want to use for a slug is still a number. I don't want to create a separate column just for the slug, since it would be identical.
I have a Lot that belongs to an Auction.
Auctions have many Lots (items for sale).
A Lot has a lot_number, which is unique to an Auction. It's not unique throughout the entire table, though. It's basically just a way to order the lots in each auction.
My URLs look like: /auctions/1/lots/21 (/auctions/:auction_id/lots/:id)
I want them to be: /auctions/1/lots/1 (/auctions/:auction_id/lots/:lot_number)
I added the following to lot.rb:
extend FriendlyId
friendly_id :lot_number
It almost works. It shows me a Lot with the correct lot number, but the wrong auction.
I read about scopes on the Friendly ID docs, which sounded perfect. I could scope the lot against the auction...
So I tried:
extend FriendlyId
friendly_id :lot_number, :use => :scoped, :scope => :auction_id
Now I see the following error:
SQLite3::SQLException: no such column: lots.slug: SELECT "lots".* FROM "lots" WHERE "lots"."slug" = '1' LIMIT 1
Why is the SQL WHERE lots.slug = 1? Shouldn't it be WHERE lots.auction_id = 1?
Am I using the wrong syntax? Not sure where I've gone wrong.
Any help would be appreciated. Thanks.
i think that you should have a look at from_param and to_param methods that are used in rails to lookup models: http://apidock.com/rails/ActiveRecord/Base/to_param
Related
Given that my slug candidate is my title, and it is already used, the slug will return something like: my-title-49c9938b-ece5-4175-a4a4-0bb2b0f26a27
Is it possible to have friendly_id return a smaller hash? Like: my-title-705d62eea60a
In that case you can create your own method, which you can pass then to friendly_id. There you can define what are going to be the combinations that FriendlyId will use in order to assign a unique identifier to your record as a slug.
For example:
friendly_id :column_candidates, use: :slugged
def column_candidates
[
:name,
[:name, :another_column],
[...more columns combinated as a fallback]
]
end
If FriendlyId can't create a unique record (by slug) after every combination of columns in column_candidates is evaluated, then it's going to append a UUID anyway.
You're free to add the objects you want to column_candidates, being strings, procs, lambdas or symbols. Also the method name doesn't have to be exactly that, you can modify it as needed.
As a last resource, and if a unique identifier can't be created, you can rely on creating your own short and always able to not be unique hash using Digest::SHA1:
...
[-> { Digest::SHA1.hexdigest(name).chars.sample(6).join }]
I am building a Rails 5 team management app which lets users manage organizations and users. I would like to be able to change from using the :id in the path (e.g: /organizations/43) and use an alpha-numeric slug instead (e.g: /organizations/H6Y47Nr7). Similar to how Trello do this (i.e: https://trello.com/b/M9X71pE6/board-name). Is there an easy way of doing this?
I have seen the FriendlyId gem which could take care of the slugging in the path but what would be the best way to generate the slug in the first place?
Ideally, for the most bang for buck the slug would include A-Z, a-z and 0-9 (as I understand it, this is Base58?) and for the sake of not blowing out the url too much, 8 characters at the most. If my calculations are correct, this gives 218 trillion combinations, which should be plenty.
Am I on the right track? Any help would be much appreciated.
Thanks
To create a slug, easiest way is to use SecureRandom. You can add something like the following in your model
before_create :generate_slug
private
def generate_slug
begin
self.slug = SecureRandom.urlsafe_base64(8)
end while Organization.exists?(slug: slug)
end
One small caveat here with respect to what you want is that the slug will sometimes contain an underscore or a dash but that should be fine.
irb(main):014:0> SecureRandom.urlsafe_base64(8)
=> "HlHHV_6rN3k"
irb(main):015:0> SecureRandom.urlsafe_base64(8)
=> "naRqT-NmYDU"
irb(main):016:0> SecureRandom.urlsafe_base64(8)
=> "9h04l4jEEsM"
If you go that route, I would create a table were you save the slugs that you are generating and don't delete them even when you delete an organization. When you create a new organization query this model to make sure there are no duplicates slugs. Also add a unique index in the slug column of the organizations table.
You should not give up the id column with integers so in the show method you will need to do:
org = Organization.where(slug: params[:id]).first
Okay so I'm working on making a friendly_id that allows me to scale it effectively. This is what I got so far. What it does is essentially add +1 on a count if you have a matching name. So if you are the 2nd person to register as John doe your url would be /john-doe-1.
extend FriendlyId
friendly_id :name_and_maybe_count, use: [:slugged, :history]
def name_and_maybe_count
number = User.where(name: name).count
return name if number == 0
return "#{name}-#{number}"
end
However this piece of code is still bringing me some issues.
If I have created a user called John Doe. Then register another user called John Doe the slug will be /john-doe-UUID. If I register a third user then it will receive the slug john-doe-1.
If I have two users. One that registered with the name first. Say Juan Pablo. Then he changes his name to 'Rodrigo', and then change it back to 'Juan Pablo'. His new slug for his original name will be 'juan-pablo-1-UUID'.
I know this is minor nitpick for most of you but it's something that I need to fix!
You want to overwrite to_param, include the id as the first bit of the friendly id
I extend active record like this in one of my apps,
module ActiveRecord
class Base
def self.uses_slug(attrib = :name)
define_method(:to_param) do
"#{self.id}-#{self.send(attrib).parameterize}"
end
end
end
so I can do
uses_slug :name
in any model
but this alone in any model should work:
def to_param
"#{self.id}-#{self.my_attrib.parameterize}"
end
The benefit of this is that in the controller you when you do
Model.find(params[:id])
you don't have to change anything because when rails tries to convert a string like "11-my-cool-friendly-id" it will just take the first numeric bits and leave the rest.
I'm current using acts_as_paranoid and friendly_id (5.0.1) on a model and when I destroy a model and try to create a new one that will generate the same slug I get:
ERROR: duplicate key value violates unique constraint "index_papers_on_slug"
I need to somehow get the code that checks if a slug already exists check within the scope of all of the objects not just the non-deleted ones.
How can I get friendly_id to use with_deleted when checking if a slug already exists. I should note that I am also using slug history which may be complicating things further.
Upon digging deeper I realized that since I am using history the slug is being fully deleted while the object is just being soft deleted:
DELETE FROM "friendly_id_slugs" WHERE "friendly_id_slugs"."id" = $1 [["id", 9423]]
So, I just need to figure out how to prevent this and I should be okay since it looks like the friendly_id code itself is already using unscoped when trying to find a valid slug.
Adding the following to the model allowed me to overrride the dependent destroy on the slugs
def has_many_dependent_for_slugs; end
The solution comes from a comment on this github issue.
Friendly_id has a module called scoped which allows you to generate unique slugs within a scope. So, probably
class Paper < ActiveRecord::Base
extend FriendlyId
friendly_id :title, :use => :scoped, :scope => :unscoped
end
will resolve the problem.
I just came across this issue too and I figured two different ways to address it.
Solution 1:
Use dependent: false.
friendly_id :title, dependent: false
Solution: 2
Overcoming this problem without overriding the dependent destroy for anyone that wants to avoid that.
The friendly_id gem uses a method called scope_for_slug_generator to set the model scope. That means we could override this method by adding the following to app/config/initializers/friendly_id.rb.
module FriendlyId
def scope_for_slug_generator
scope = if self.class.base_class.include?(Paranoia)
self.class.base_class.unscoped.with_deleted
else
self.class.base_class.unscoped
end
scope = self.class.base_class.unscoped
scope = scope.friendly unless scope.respond_to?(:exists_by_friendly_id?)
primary_key_name = self.class.primary_key
scope.where(self.class.base_class.arel_table[primary_key_name].not_eq(send(primary_key_name)))
end
end
Another question from rails newbie. I am using friendly_id gem with mysql in rails 3.x
This is a design problem (may be easy in rails). I am expecting advises from rails experts. I am building a library listing app. Where user can view library by "metro-area" or by "city" in it. For example:
I wish to have URLs like:
www.list.com/library/san-francisco-bay-area
or
www.list.com/library/san-francisco-bay-area/palo-alto/
In database I have tables:
library
-------
id, name, city_id, slug
name is slugged here and city_id is FK
city
----
city_id, name, metro_area_id, slug
name is slugged here and metro_area_id is FK
metro_area
----------
metro_area_id, name, state, slug
name is slugged here
So when a user points browser to www.list.com/library/san-francisco-bay-area/palo-alto
I wish to get list of libraries in san-francisco-bay-area/palo-alto. But my library table model is containing slug only for library's name. So how this URL can be parsed to find the city_id that can be used in library model and controller to get the list.
Please remember, I cannot rely only on the name of the city. I would have to find city_id of 'palo-alto' which is in metro 'san-francisco-bay-area'. Since slug for metro_area and city is in other tables, so what is the best way to design model and controller.
Show method of controller is:
def show
#user = Library.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #library }
end
end
and model is
class Library < ActiveRecord::Base
attr_accessible :name, :city_id, :slug
extend FriendlyId
friendly_id :name, use: :slugged
end
This will not work per my friendly URL requirement. So I would appreciate advice from experts :)
Maybe you have found your solution, I'm new to rails as well, so I'm just guessing this would work out.
Since you wanna display slugs from two different models. I'm assuming the way to display ids would be something like
www.list.com/libraries/:id/cities/:id/metro_areas/:id
Which can be done through editing the route file by adding Nested Resources
As for just displaying two ids like
www.list.com/library/:city_id/:metro_area_id
Rails guide refers it as Dynamic Segments
After that, it's just a matter of converting the ids to slugs.
I also found this in FriendlyId's documentation which is addressing to your case.
Hope it helps