Hello,
I am new here and actually I am writing my first RoR app.
My question is:
--------
class Topic < ActiveRecord::Base
has_many :messages
end
class Message < ActiveRecord::Base
belongs_to :topic
end
class ForumController < ApplicationController
def post
message = Message.new(params[:message])
@topic = Topic.find(params[:id])
@topic.messages << message
end
end
Okay, this works. However, if i write
message = Message.new(params[:message])
@topic = Topic.find(params[:id])
@topic.messages << message
@topic.messages << message
(last line repeated once), then I got an primary key constraint error
message.
I understand the database message. I thought this will help:
@topic.messages << message.dup
@topic.messages << message.dup
but it didn''t.
I checked that the class of @topic.messages is Array, and I didn''t find
any description for the << operator in the rails manual. Does it some
special, different from Ruby? When is this arra saved (or the new
element inserted) to the database? How can I append more then one
element to the end of this ActiveRecord array?
Mage
Operator << is the standard Ruby append/push operation On 2/13/06, Mage <mage@mage.hu> wrote:> Hello, > > I am new here and actually I am writing my first RoR app. > > My question is: > > -------- > class Topic < ActiveRecord::Base > has_many :messages > end > > class Message < ActiveRecord::Base > belongs_to :topic > end > > class ForumController < ApplicationController > def post > message = Message.new(params[:message]) > @topic = Topic.find(params[:id]) > @topic.messages << message > end > end > > Okay, this works. However, if i write > > message = Message.new(params[:message]) > @topic = Topic.find(params[:id]) > @topic.messages << message > @topic.messages << message > > (last line repeated once), then I got an primary key constraint error > message. > > I understand the database message. I thought this will help: > > @topic.messages << message.dup > @topic.messages << message.dup > > but it didn''t. > > I checked that the class of @topic.messages is Array, and I didn''t find > any description for the << operator in the rails manual. Does it some > special, different from Ruby? When is this arra saved (or the new > element inserted) to the database? How can I append more then one > element to the end of this ActiveRecord array? > > Mage > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Well, I found that when I write:
active_record.subitems << subitem
the new subitem will be inserted to the database immediately. Somewhere
must be a method which does this. But where?
Mage
Zachary Zolton wrote:
>Operator << is the standard Ruby append/push operation
>
>On 2/13/06, Mage <mage@mage.hu> wrote:
>
>
>> Hello,
>>
>>I am new here and actually I am writing my first RoR app.
>>
>>My question is:
>>
>>--------
>>class Topic < ActiveRecord::Base
>> has_many :messages
>>end
>>
>>class Message < ActiveRecord::Base
>> belongs_to :topic
>>end
>>
>>class ForumController < ApplicationController
>> def post
>> message = Message.new(params[:message])
>> @topic = Topic.find(params[:id])
>> @topic.messages << message
>> end
>>end
>>
>>Okay, this works. However, if i write
>>
>> message = Message.new(params[:message])
>> @topic = Topic.find(params[:id])
>> @topic.messages << message
>> @topic.messages << message
>>
>>(last line repeated once), then I got an primary key constraint error
>>message.
>>
>>I understand the database message. I thought this will help:
>>
>> @topic.messages << message.dup
>> @topic.messages << message.dup
>>
>>but it didn''t.
>>
>>I checked that the class of @topic.messages is Array, and I
didn''t find
>>any description for the << operator in the rails manual. Does it
some
>>special, different from Ruby? When is this arra saved (or the new
>>element inserted) to the database? How can I append more then one
>>element to the end of this ActiveRecord array?
>>
>> Mage
>>
>>
>>_______________________________________________
>>Rails mailing list
>>Rails@lists.rubyonrails.org
>>http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>>
>>
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
Matt Jankowski wrote:> You''re trying to add the same message twice?Yes. After I sent this e-mail I have noticed that if I write: mdup = message.dup @topic.messages << mdup then somehow object message also will change after inserting mdup. (message also will get mdup''s new id which was null before the insert) however with: mdup = message.clone @topic.messages << mdup original message object won''t change after instering mdup. This is a little weird to me, but I am new to Ruby too. Mage
take a look at the has_many documentation: http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000471 also see the section titled "Unsaved objects and associations" subheading "Collections" toward the top of the page for additional information. On 2/13/06, Zachary Zolton <zachary.zolton@gmail.com> wrote:> > Operator << is the standard Ruby append/push operation > > On 2/13/06, Mage <mage@mage.hu> wrote: > > Hello, > > > > I am new here and actually I am writing my first RoR app. > > > > My question is: > > > > -------- > > class Topic < ActiveRecord::Base > > has_many :messages > > end > > > > class Message < ActiveRecord::Base > > belongs_to :topic > > end > > > > class ForumController < ApplicationController > > def post > > message = Message.new(params[:message]) > > @topic = Topic.find(params[:id]) > > @topic.messages << message > > end > > end > > > > Okay, this works. However, if i write > > > > message = Message.new(params[:message]) > > @topic = Topic.find(params[:id]) > > @topic.messages << message > > @topic.messages << message > > > > (last line repeated once), then I got an primary key constraint error > > message. > > > > I understand the database message. I thought this will help: > > > > @topic.messages << message.dup > > @topic.messages << message.dup > > > > but it didn''t. > > > > I checked that the class of @topic.messages is Array, and I didn''t find > > any description for the << operator in the rails manual. Does it some > > special, different from Ruby? When is this arra saved (or the new > > element inserted) to the database? How can I append more then one > > element to the end of this ActiveRecord array? > > > > Mage > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060213/bac6cc45/attachment.html