Dorijan Jelincic
2011-Aug-25 17:39 UTC
ruby on rails 3, active record, how to address data
Hi to all, have active record model that has one to many relationship. This is how it looks like: ruby-1.9.2-p290 :001 > @ukuizvj=Izvjestaj.new(:mjesec=>8, :godina=>2011,:djelatnik_id=>2) => #<Izvjestaj id: nil, godina: 2011, mjesec: 8, djelatnik_id: 2, brojsati: nil, visak: nil, manjak: nil, nr: nil, rp: nil, komb: nil, created_at: nil, updated_at: nil> ruby-1.9.2-p290 :002 > @ukuizvj.izvjtipd => [] ruby-1.9.2-p290 :003 > ntiprada=Izvjtipd.new(:tipneradni_id=>1, :vrijednost=>1) => #<Izvjtipd id: nil, izvjestaj_id: nil, tipneradni_id: 1, vrijednost: 1, created_at: nil, updated_at: nil> ruby-1.9.2-p290 :004 > @ukuizvj.izvjtipd << ntiprada => [#<Izvjtipd id: nil, izvjestaj_id: nil, tipneradni_id: 1, vrijednost: 1, created_at: nil, updated_at: nil>] ruby-1.9.2-p290 :005 > @ukuizvj.izvjtipd => [#<Izvjtipd id: nil, izvjestaj_id: nil, tipneradni_id: 1, vrijednost: 1, created_at: nil, updated_at: nil>] So, How can I address this value of @ukuizvj.izvjtipd? Problem is that @ukuizvj.izvjtipd.find_by_tipneradni_id(1).vrijednost or @ukuizvj.izvjtipd.where(:tipneradni_id=>1).vrijednost dont work until I make @ukuizvj.save Thank you -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jim Ruther Nill
2011-Aug-28 00:27 UTC
Re: ruby on rails 3, active record, how to address data
On Fri, Aug 26, 2011 at 1:39 AM, Dorijan Jelincic <dmailj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi to all, > have active record model that has one to many relationship. This is > how it looks like: > > ruby-1.9.2-p290 :001 > > @ukuizvj=Izvjestaj.new(:mjesec=>8, :godina=>2011,:djelatnik_id=>2) > => #<Izvjestaj id: nil, godina: 2011, mjesec: 8, djelatnik_id: 2, > brojsati: nil, visak: nil, manjak: nil, nr: nil, rp: nil, komb: nil, > created_at: nil, updated_at: nil> > > > ruby-1.9.2-p290 :002 > @ukuizvj.izvjtipd > => [] > > > ruby-1.9.2-p290 :003 > > ntiprada=Izvjtipd.new(:tipneradni_id=>1, :vrijednost=>1) > => #<Izvjtipd id: nil, izvjestaj_id: nil, tipneradni_id: 1, > vrijednost: 1, created_at: nil, updated_at: nil> > > > > ruby-1.9.2-p290 :004 > @ukuizvj.izvjtipd << ntiprada > => [#<Izvjtipd id: nil, izvjestaj_id: nil, tipneradni_id: 1, > vrijednost: 1, created_at: nil, updated_at: nil>] > > > > ruby-1.9.2-p290 :005 > @ukuizvj.izvjtipd > => [#<Izvjtipd id: nil, izvjestaj_id: nil, tipneradni_id: 1, > vrijednost: 1, created_at: nil, updated_at: nil>] > > So, How can I address this value of @ukuizvj.izvjtipd? Problem is that > @ukuizvj.izvjtipd.find_by_tipneradni_id(1).vrijednost or > @ukuizvj.izvjtipd.where(:tipneradni_id=>1).vrijednost dont work until > I make @ukuizvj.save > >find_by_attribute and where statements both create sql statements which are executed when you access the records. since the record you''re looking for is not yet saved to the db, AR won''t find it using these methods.> Thank you > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.