In PostgreSQL, I have a view with support for all CRUD operations through a nice set of rules. This view is one of many views which are necessary to keep this database (somewhat) manageable. I''m trying to use this view as the relation for a model. Now, when I try to assign values to instances of this class, I get "NoMethodError: undefined method `name=''" where name is the name of the field I''m trying to assign to.>From http://wiki.rubyonrails.com/rails/show/HowtoUsePostgresViewsAsTables,I got the impression that using views to back models is quite possible, so what could I be doing wrong here? It seems as if Rails doesn''t define the assignment methods because it assumes views just aren''t writeable or something. Clueless, - Rowan -- Morality is usually taught by the immoral.
The problem is more likely that it''s looking for the wrong table (view in your case) and therefore doesn''t know what the columns are (ie no methods are created). Try doing a find(:all) first to make sure you are looking at the right table. Plurilization (sp??) can sometimes make life a little difficult. If AR is looking in the wrong spot then the find all should bomb with a "unknown table" or something like that error. If that happens use set_table_name to have it point in the right direction. John W Higgins wishdev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
On 6/27/05, John W Higgins <wishdev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The problem is more likely that it''s looking for the wrong table (view > in your case) and therefore doesn''t know what the columns are (ie no > methods are created). > > Try doing a find(:all) first to make sure you are looking at the right > table. Plurilization (sp??) can sometimes make life a little difficult. > If AR is looking in the wrong spot then the find all should bomb with a > "unknown table" or something like that error. If that happens use > set_table_name to have it point in the right direction.I have been doing a little bit more debugging yesterday night and discovered that Update operations do work. It''s just creating new records that doesn''t work. Wether I call MyModel.new() with all the attributes or call MyModel.new and then try to assign an attribute, I always get this same error. Select operations had already been working just fine all the time. I was indeed already using set_table_name because the table didn''t obey the pluralization rules. - Rowan -- Morality is usually taught by the immoral.
On 6/27/05, BigSmoke <bigsmoke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 6/27/05, John W Higgins <wishdev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > The problem is more likely that it''s looking for the wrong table (view > > in your case) and therefore doesn''t know what the columns are (ie no > > methods are created). > > > > Try doing a find(:all) first to make sure you are looking at the right > > table. Plurilization (sp??) can sometimes make life a little difficult. > > If AR is looking in the wrong spot then the find all should bomb with a > > "unknown table" or something like that error. If that happens use > > set_table_name to have it point in the right direction. > > I have been doing a little bit more debugging yesterday night and discovered > that Update operations do work. It''s just creating new records that doesn''t > work. Wether I call MyModel.new() with all the attributes or call > MyModel.new and then try to assign an attribute, I always get this same error. > > Select operations had already been working just fine all the time. > I was indeed already using set_table_name because the table didn''t obey the > pluralization rules.Apparently the problem was that I had removed the "<schemaname>." prefix from the table name in the model. I had assumed that this would be no problem after I set the correct schema order in config/database.yml. Clearly, I assumed wrong. Only today I realized the error in my ways, after I thought: Where does Active Record have to get information which columns are supported for a given relation? It can get this information from information_schema.columns and there each column is identified not only by a table name and a column name, but also a schema name. And I can hardly assume that Active Record will traverse every schema in the schema path to find out where the table specified by ActiveRecord::Base#set_table_name resided, now can I? Everything works much better now. Well, not everything, but these are all topics for future messages :) Rowan -- Morality is usually taught by the immoral.
Jeremy Kemper
2005-Jun-29 17:07 UTC
Re: Storing to PostgreSQL views in Active Record - Solved
On Jun 29, 2005, at 9:55 AM, BigSmoke wrote:> Apparently the problem was that I had removed the "<schemaname>." > prefix > from the table name in the model. I had assumed that this would be > no problem > after I set the correct schema order in config/database.yml. > Clearly, I assumed wrong.Have you seen http://dev.rubyonrails.org/ticket/827 ? It adds better schema support for just this case and will be available in the next release (in beta gems now.) Best, jeremy