On Friday, March 11, 2016 at 11:20:02 AM UTC-8, Denny Wang wrote:
>
> Hi Jeremy,
>
> I had successfully make it working but ran into another problem. When I 
> calling column of a dataset, it raises a NoMethodError. However, using 
> something like data[:name] is fine.
>
> DB = Sequel.mock
>
> DB.create_table(:artists) do
>  # Primary key must be set explicitly
>  primary_key :id
>  String :name
> end
>
> DB.create_table(:albums) do
>  primary_key :id
>  # Table that foreign key references needs to be set explicitly
>  # for a database foreign key reference to be created.
>  foreign_key :artist_id, :artists
>  String :name
> end
>
> class Artists < Sequel::Model(:artists)
>  one_to_many :albums
> end
>
> class Album < Sequel::Model(:albums)
>  many_to_one :artists
> end
>
> class Tag < Sequel::Model
>  many_to_many :albums
> end
>
>
> describe 'Model testing' do
>
>  before do
>  ds = Artists.dataset
>  ds.columns([:id, :name])
>  ds._fetch = [{:id => 1, :name =>'name1'}]
>  ds1 = Album.dataset
>  # ds.columns([:id, :name])
>  ds1._fetch = [{:id => 2,
>  :artist_id => 1,
>  :name => 'name2'
>  }]
>  end
>
>  it 'this is a test to test test' do
>
>  data = Artists[:id => 1]
>
>  expect(data.id).to eq(1)
>  expect(data.name).to eq ('name1')
>  expect(data.albums[0].name).to eq('name2')
>  end
> end
>
> This give the error:
>
> NoMethodError: undefined method `name' for #<Artists @values={:id=>1, 
> :name=>"name1"}>
>
>
First, there is no reason to create tables when using a mocked database, as 
it doesn't do anything.

The reason for your error is because when creating the Model you haven't 
given it a dataset with the columns set, and the query to return the 
columns doesn't yield the correct info.  The easiest way to fix it is to 
tell the mocked database which columns to return:

DB.columns = proc do |sql|
  case sql
  when /artists/, /tags/ then [:id, :name]
  when /albums/ then [:id, :artist_id, :name]
  else []
  end
end

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