On 15 October 2010 14:26, Jarl Friis <[email protected]> wrote:
> Jarl Friis <[email protected]> writes:
>
>> Thanks Xuan and Colin for your attention.
>>
>> Xuan <[email protected]> writes:
>>
>>> You may try adding a "default_scope :select=>'id, name, etc'  " to
>>> your model. This would accomplish what you are looking for,
>>
>> That was exactly what I was looking for.
>>
>> My actual implentation looked like this in my AR class
>>
>>   DEFAULT_SELECT = "#{column_names.reject{|c| c =~ /image/ }.map{|c| 
>> "\"orders\".\"#{c}\""}.join(', ')}".freeze
>>   default_scope :select => DEFAULT_SELECT
>
> Warning to others:
>
> This is not a good solution, it gives problems upon deploying an
> application at a clean environment. The problem arise when doing rake
> db:migrate, since column_names tries to get column names from the
> database at a time where the table does not exist!

I think you can avoid the problem in the migration by defining the
model class inside the migration, so something like:

class YourMigration < ActiveRecord::Migration
  class TheProblematicClass < ActiveRecordBase;
  end

  def self.up
    ...

This stops your class definition being used by the migration so your
default scope is never used.  In fact I believe it is good to do this
if you ever reference an AR class in the migration as the code for the
class may not match the current state of the db during migrations.

Colin

Colin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: 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/rubyonrails-talk?hl=en.

Reply via email to