On Friday, September 29, 2017 at 3:26:22 PM UTC-7, French Fry wrote:
>
> I used this example from the read me (using my own tables / columns) and 
> it works fine.  Foreign key picks up the parent table primary key.  I did 
> this in bin/sequel.
>
> post = Post.create(name: 'hi!')post.comments
> comment = Comment.create(text: 'hi')post.add_comment(comment)
>
>
> However in my app code, trying to do the same thing in an iteration,  the 
> 'add_comment' throws
> the error - `block in grab_files': undefined method `add_folder' for 
> nil:NilClass (NoMethodError)
>
> Here is the method (it's ugly, and perhaps the error is do to some screwy 
> logic).  To explain though the way the code traverses is it picks up the 
> folder path and then the individual files, in other words they should be in 
> order.  First path, then a bunch of folders, then it should start the cycle 
> again.  
>
> Here is the classes - 
>
>
> class Path < Sequel::Model
>   plugin :validation_helpers
>   one_to_many :folders
>
>
>   def validate
>     super
>     validates_presence [:path]
>   end
> end
>
>
> class Folder < Sequel::Model
>   many_to_one :path
> end
>
> And here the logic - 
>
> def grab_files
>   music_list = Dir.glob("/vagrant/TestRails/Music/**/*")
>   music_list.each  do |x| 
>     if File.ftype(x) == "directory"
>       path = Path.create(path: x)
>        path.folders
>     elsif File.ftype(x) == "file"
>        folder = Folder.create(folder_name: (File.basename(x)))
>          path.add_folder(folder)
>

path is not set here, so the value of path here is nil, hence the 
NoMethodError add_folder for nil:NilClass

I'm guessing you should add something like:

  path = Path.create(path: x)

to the elsif branch or move the existing Path.create call to before the if 
branch.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to