Jumping through hoops using Spree models to seed store data

89 views
Skip to first unread message

bensullivan

unread,
Apr 30, 2012, 5:45:50 PM4/30/12
to spree...@googlegroups.com
Hi folks

I am developing a new spree store for a client who only has about 15 products currently so I just need to get them in without having to do it through the UI every time.

I've been looking at the seeds.rb file in my new store generated by the spree install command.

It seems like the place to do this kind of stuff and then plugging it in using the Rails rakes tasks.

I figured it's safer using the Spree active_record models to import data as I only have maybe 15 or so products and I'll get all the data validation moving forward.  I have seen the spree_sample gem which seems to use yaml to get the demo store data in.  I was going to use that as the backup plan.

So I'm trying to put a basic record creation line of code in the seeds.rb file:

p = Product.create :name => "Moo", :price => 10.0, :description => "meeeeep"

I run "rake db:bootstrap" and I get this at the end of the output:

rake aborted!
uninitialized constant Product

So then this triggers me to put in all the necessary requires - that's cool.  But then I find there's a few transitive dependencies and the model classes also need to be added to the load path for me to get to them:

require 'rubygems'

gem 'spree_core'
directory = '/Users/bensullivan/.rvm/gems/ruby-1.9.3-p125@geometry_active_wear/gems/spree_core-1.1.0'
$LOAD_PATH.unshift(directory) unless $LOAD_PATH.include?(directory)

require 'active_record'
require 'active_support/core_ext/module/attribute_accessors'
require 'spree/core/delegate_belongs_to'
require 'app/models/spree/variant'
require 'app/models/spree/product'

p = Product.create :name => "Moo", :price => 10.0, :description => "meeeeep"

I get the same error when I retry the db:bootstrap.  I don't get the loading error I see through rake when I run this script in isolation (instead I get an error around the database not running which makes more sense cos it isn't).  I seem to have to go through a lot of hoop jumping to just create a Product using a spree Model.  Which makes me think either I'm doing it wrong or if the API isn't making it easy for me to do then I maybe I shouldn't be doing it at all.

I feel like I'm missing something here - I'm sure there's an easier "Railsy" way to do this.

Anyone got any pointers?

Cheers

Ben

Joseph Jackson

unread,
Apr 30, 2012, 5:49:12 PM4/30/12
to spree...@googlegroups.com
Looks like it needs to be namespaced.  

Spree::Product.create etc.


--
You received this message because you are subscribed to the Google Groups "Spree" group.
To view this discussion on the web visit https://groups.google.com/d/msg/spree-user/-/QvFOlcgREisJ.
To post to this group, send email to spree...@googlegroups.com.
To unsubscribe from this group, send email to spree-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spree-user?hl=en.

bensullivan

unread,
Apr 30, 2012, 5:57:01 PM4/30/12
to spree...@googlegroups.com
Aaaaarrrrrrggggghhhhhh! (facepalm).

That worked!

Thanks so much.

Is there a way to do this that requires less song and dance around the single line of code I'm interested in in the seeds file?

Do folks tend to not use active_record models directly to import seed data and defer to straight yaml and tools like datashift?

Cheers again

Ben
To unsubscribe from this group, send email to spree-user+unsubscribe@googlegroups.com.

Joseph Jackson

unread,
Apr 30, 2012, 5:59:46 PM4/30/12
to spree...@googlegroups.com
I'm not sure.  My clients opted to add all the products in by hand rather than pay me to import their old data.  They are still working on it...

I do hear a lot about datashift on this group though.

To view this discussion on the web visit https://groups.google.com/d/msg/spree-user/-/m4d2D4rP0jUJ.

To post to this group, send email to spree...@googlegroups.com.
To unsubscribe from this group, send email to spree-user+...@googlegroups.com.

Ben Sullivan

unread,
Apr 30, 2012, 6:02:14 PM4/30/12
to spree...@googlegroups.com
OK thanks
Reply all
Reply to author
Forward
0 new messages