On Jun 17, 10:06 am, Iain Barnett <[email protected]> wrote: > I got halfway through this when life had other plans for me, but I'm back to > it and I'm having some problems, I'm not sure if it's to do with the > migration or the model I've set up. > > I've set up a very simple spec just to get started with - set up the db, a > model, and save a instance of the model. I'm trying out the models with the > Twitter bot I had running with non-model sequel, but on running rspec I get > this error message: > > Failures: > > 1) TwitterSearchBot::Tweet save > Failure/Error: Tweet.new( { > Sequel::Error: > method profile_image_url= doesn't exist or access is restricted to it > > I check via logger that the tables exists and the schema for Tweets is > correct, and it is. > > Here's the model: > > module TwitterSearchBot > class Tweet < Sequel::Model > require 'digest/sha2' > > def before_create > self.signature ||= Digest::SHA2.hexdigest( self.text_of ) > super > end # def > end > end > > the migration: > > Sequel.migration do > up do > create_table(:tweets) do > primary_key :tweets_id > Bignum :id, :null=>false > Integer :from_user_id > String :from_user, :size=>15, :null=>false > String :profile_image_url, :size=>120 > DateTime :created_at, null: false, default: DateTime.now > String :text_of, :size=>200, :null=>false > String :iso_language_code, :size=>2, :fixed=>true > String :source, :size=>160 > String :signature, size:64, :fixed=>true > unique [:id, :signature] > end > end > > down do > drop_table(:tweets) > end > end > > and here's the spec: > > require 'rspec' > require 'sequel' > > module TwitterSearchBot > > DB = Sequel.sqlite #File.join( File.dirname(__FILE__), 'test.db' ) # I > initially tried it with an in-memory db, but a file is no different to the > outcome > > require_relative "../lib/twittersearchbot/models/tweets.rb" > > describe Tweet do > before(:all){ > Sequel.extension :migration > Sequel::Migration.descendants.clear > require_relative '../migrations/001_project.rb' > Sequel::Migration.descendants.each{|m| m.apply(DB, :up)} > }
Try moving your requiring the model to right before the end of the before(:all) block. Alternatively, do Tweet.dataset = Tweet.dataset there instead. The way Sequel works is that when the model subclass is defined, Sequel parses the database schema in order to set up the column accessors. So if you define the model before the table exists, and then create the table, the class won't have any column accessors defined. Creating the table before defining the model will work, as will setting anew dataset (even to the existing value), as that causes a new schema parse. I will say that unless you have a good reason to run your migrations inside of your specs, I would recommend against that approach. I recommend you migrate your test database like you migrate your development database, and then you can just run your specs without worrying about these things. Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sequel-talk?hl=en.
