Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails Tableless Model Associations with Legacy Database
    primarykey
    data
    text
    <p>I'm just learning RoR, and am trying to build a Model around my legacy database, which is built more around SPROCs for querying data. I've found the <a href="http://rubygems.org/gems/activerecord-tableless" rel="nofollow">activerecord-tableless</a> gem and am using that to help build the models.</p> <p>So far, I'm able to get the base model working OK:</p> <pre><code>class Wine &lt; ActiveRecord::Base has_no_table self.primary_key = "iWine" column :iWine, :integer column :Wine, :string column :Vintage, :integer ....etc...... attr_accessible :iWine, :Wine, :Vintage, ..... has_many :labelImages def self.find_wine(id) r = ActiveRecord::Base.execute_procedure "sp_FetchWineVerbose", 'iWine' =&gt; id.to_i if r.size &gt; 0 w = Wine.new(r[0]) return w; end end end </code></pre> <p>Now, I'd like to take advantage of ActiveRecord's associations to pull in additional pieces of related data, e.g. label images, other vintages, etc. Here is what I have so far:</p> <pre><code>class LabelImage &lt; ActiveRecord::Base has_no_table column :id, :integer column :width, :integer column :height, :integer column :wine_id, :integer attr_accessible :id, :width, :height, :wine_id after_initialize :fetch_data belongs_to :wine def fetch_data() sql = &lt;&lt;-eos SELECT iLabel AS id, Width AS width, Height AS height, .... eos r = ActiveRecord::Base.connection.select_all(sql, 'Label Image', [[wine_id,wine_id]]) if r.size &gt; 0 self.assign_attributes(r[0]) end end end </code></pre> <p>So, now, I can call <code>w = Wine.find_wine(1)</code> and then <code>w.labelImages.build</code>, and I get back a LabelImage object with the right data. But, I also get the following message in the console:</p> <pre><code>Could not log "sql.active_record" event. NoMethodError: undefined method `name' for 1:Fixnum </code></pre> <p>I've tried digging through the source code but cannot figure out where this is coming from. And, I also can't figure out how to override the initialization to return an array of multiple LabelImage objects -- as there may be many for any given wine. Should I override the <code>build</code> method (and if so, how?), or is there another way to create the objects and then assign them to the Wine.labelImages attribute?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload