friendly_id Model.find_each(&:save)

25 views
Skip to first unread message

fugee ohu

unread,
Jan 25, 2019, 9:15:18 PM1/25/19
to Ruby on Rails: Talk
Model.find_each(&:save) 
This command doesn't work for me, each commit causes ROLLBACK and at the end an error from active storage " invalid constant name 'admin' " what does the &:save actually mean?

Walter Lee Davis

unread,
Jan 26, 2019, 12:40:12 PM1/26/19
to rubyonra...@googlegroups.com

> On Jan 25, 2019, at 9:15 PM, fugee ohu <fuge...@gmail.com> wrote:
>
> Model.find_each(&:save)
> This command doesn't work for me, each commit causes ROLLBACK and at the end an error from active storage " invalid constant name 'admin' " what does the &:save actually mean?

Symbol to Proc is what this is called. It's a language feature in Ruby that lets you apply a method to a number of objects within an enumerable structure. To illustrate, here's a trivial example:

%w[foo bar baz] &:upcase
--> ['FOO', 'BAR', 'BAZ']

Array goes in, each element of the array has upcase called against it, and a new array is returned containing the upper-case results. If you called &:upcase! instead, the same array would be returned, with the values within it mutated in place.

It's similar to calling map() and passing a block.

Getting back to your error message, do you, by any chance, have a column in your model named (exactly) `type`? If so, and you have a single-table-inheritance setup or a polymorphic join, and you try to add a lower-case string to that column, that could explain the error. The values that go into type should be contantized (initial cap). I'm not sure if the values are checked against the other model names in the application, but if they are, then you would also have to add a constant-shaped value that actually matched one of the other models in your application.

Walter


>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/60026bb3-7508-45e5-95b7-558e36734b04%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

fugee ohu

unread,
Jan 26, 2019, 2:18:58 PM1/26/19
to Ruby on Rails: Talk
 class PressRelease < ApplicationRecord

  extend FriendlyId
  friendly_id :truncated_headline, use: :slugged
  belongs_to :poster, polymorphic: true
  attr_accessor :club_id
  attr_accessor :festival_id
  attr_accessor :user_venue_id
  has_many :pictures, as: :imageable

  def self.search(search)
    where("headline LIKE ? OR storyline LIKE ? OR publicist LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%")
  end
  
  def truncated_headline 
  # cut off at 200 characters, without an ellipsis 
headline.truncate(255, '') 
  end 

Reply all
Reply to author
Forward
0 new messages