Hi. I have a problem with the association methods which passed to a model through a belongs_to declaration. Here''s an illustration of the issue: GIVEN: # migration class CreateArticlesAndAuthorsTables < ActiveRecord::Migration def self.up create_table :articles do |t| t.text :title t.integer :author_id end create_table :authors do |t| t.text :name end end def self.down drop_table :articles drop_table :authors end end # articles model class Article < ActiveRecord::Base belongs_to :author end # authors model class Author < ActiverRecord::Base has_many :articles end WHEN:>> Article.create(:title => ''one'').author = Author.create(:name => ''Pavel'')>> Article.create(:title => ''two'').author = Author.find(:first, :conditions => {:name => ''Pavel''})THEN sqlite> select * from authors; id = 1 name = Pavel sqlite> select * from articles; id = 1 title = ''one'' author_id = null id = 2 title = ''two'' author_id = null Why do I have null values instead of ids as foreign keys in the articles table? Thanks. Ruby 1.9.2; Rails 2.3.8. -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I got it. It should to break the creation statements onto a few steps: WHEN:>> one = Article.new(:title => ''one'')>> one.author = Author.new(:name => ''Pavel'')>> one.save>> two = Article.new(:title => ''two'')>> two.author = Author.find(:first, :conditions => {:name => ''Pavel''})>> two.saveTHEN: sqlite> select * from authors; id = 1 name = Pavel sqlite> select * from articles; id = 1 title = ''one'' author_id = 1 id = 2 title = ''two'' author_id = 1 On 29 июн, 19:30, "P.A." <shama...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> Hi. > > I have a problem with the association methods which passed to a model > through a belongs_to declaration. Here''s an illustration of the issue: > > GIVEN: > > # migration > class CreateArticlesAndAuthorsTables < ActiveRecord::Migration > def self.up > create_table :articles do |t| > t.text :title > t.integer :author_id > end > > create_table :authors do |t| > t.text :name > end > end > > def self.down > drop_table :articles > drop_table :authors > end > end > > # articles model > class Article < ActiveRecord::Base > belongs_to :author > end > > # authors model > class Author < ActiverRecord::Base > has_many :articles > end > > WHEN: > > >> Article.create(:title => ''one'').author = Author.create(:name => ''Pavel'') > >> Article.create(:title => ''two'').author = Author.find(:first, :conditions => {:name => ''Pavel''}) > > THEN > > sqlite> select * from authors; > id = 1 > name = Pavel > > sqlite> select * from articles; > id = 1 > title = ''one'' > author_id = null > > id = 2 > title = ''two'' > author_id = null > > Why do I have null values instead of ids as foreign keys in the > articles table? > > Thanks. > > Ruby 1.9.2; > Rails 2.3.8.-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Jun-29 16:47 UTC
Re: Re: belongs_to. Association methods don''t pass data to DB
On 29 June 2010 17:29, P.A. <shamaoke-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> I got it. > > It should to break the creation statements onto a few steps: > > WHEN: > >>> one = Article.new(:title => ''one'') > >>> one.author = Author.new(:name => ''Pavel'') > >>> one.save > >>> two = Article.new(:title => ''two'') > >>> two.author = Author.find(:first, :conditions => {:name => ''Pavel''}) > >>> two.saveHave a look at author.articles.build which will build a new article for an author and save some messing about. 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 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.
I was most interested about that methods which are added by belongs_to declaration not has_many. Anyway, thanks for help, Colin. On 29 июн, 20:47, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 29 June 2010 17:29, P.A. <shama...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: > > > > > I got it. > > > It should to break the creation statements onto a few steps: > > > WHEN: > > >>> one = Article.new(:title => ''one'') > > >>> one.author = Author.new(:name => ''Pavel'') > > >>> one.save > > >>> two = Article.new(:title => ''two'') > > >>> two.author = Author.find(:first, :conditions => {:name => ''Pavel''}) > > >>> two.save > > Have a look at > author.articles.build > which will build a new article for an author and save some messing about. > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.