HI, let''s get straight to my question: Let''s say that I have 2 tables: ''articles'' and ''notes'' articles) id title description .... note_id (default = 0) notes) id text The relation between Article and Note is 1-to-1, but not every Article has a note. Articles with note_id = 0 do not have any note. I want to access article''s note in a simple and clean way; to do that I have this definition in my model/article.rb file: def note unless note_id = 0 Note.find(note_id).text end end Nice? Well... it looks nice to me, but it doesn''t work as I would expect. for instance: <%= @article.note %> won''t display ''Note.find(@artcile.note_id).text'' as expected Am I missing something here? How can I express this kind of relationship in the model? Cheers. /Giulio
Giulio Mola wrote:> HI, > > let''s get straight to my question: > > Let''s say that I have 2 tables: ''articles'' and ''notes'' > articles) > id > title > description > .... > note_id (default = 0) > > notes) > id > text > > > The relation between Article and Note is 1-to-1, but not every Article > has a note. Articles with note_id = 0 do not have any note. > > I want to access article''s note in a simple and clean way; to do that > I have this definition in my model/article.rb file: > def note > unless note_id = 0 > Note.find(note_id).text > end > end > > > Nice? > Well... it looks nice to me, but it doesn''t work as I would expect. > > for instance: > <%= @article.note %> > won''t display ''Note.find(@artcile.note_id).text'' as expected > > Am I missing something here? > How can I express this kind of relationship in the model? > > Cheers. > /Giulio > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >Have you set the AR relationships in your models? class Article has_one :note end class Note has_one :article end Then you shouldn''t need your note method as AR does all the work for you. Chris
On 8/25/05, Giulio Mola <lamola-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> HI, > > let''s get straight to my question: > > Let''s say that I have 2 tables: ''articles'' and ''notes'' > articles) > id > title > description > .... > note_id (default = 0) > > notes) > id > text > > > The relation between Article and Note is 1-to-1, but not every Article > has a note. Articles with note_id = 0 do not have any note. > > I want to access article''s note in a simple and clean way; to do that > I have this definition in my model/article.rb file: > def note > unless note_id = 0 > Note.find(note_id).text > end > end > > > Nice? > Well... it looks nice to me, but it doesn''t work as I would expect. > > for instance: > <%= @article.note %> > won''t display ''Note.find(@artcile.note_id).text'' as expected > > Am I missing something here? > How can I express this kind of relationship in the model?You may want to try moving your note related model methods into note.rb, rather than article.rb.
Hi, Giulio Mola wrote:> Let''s say that I have 2 tables: ''articles'' and ''notes'' > articles) > id > title > description > .... > note_id (default = 0) > > notes) > id > text > > The relation between Article and Note is 1-to-1, but not every Article > has a note. Articles with note_id = 0 do not have any note. >If the relationship is indeed 1-to-1 (i.e. you wouldn''t have the same note in different articles) I would revert foreign key: put an article_id field into the notes table and remove the note_id from the articles Then you could have: class Article< ActiveRecord::Base has_one :note end class Note< ActiveRecord::Base belongs_to :article end in this way rails adds to both the classes methods to let you access the associated object (and to test its existance): e.g. if a is an Article: - a.note gets the associated note - a.note.nil? returns true if the article has an associated note etc. etc. (see: http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html for a complete documentation of associations) HTH Luca