Shouldn't this insert in table?

in MyAController I want to create an instance of MyBModel, and have it insert into the table for the MyB models

So, in a function in MyAController, I have

MyB myB = MyB.new myB.something = whatever myB.save

Shouldn't that have inserted a new row into the table for MyB models?

it will if validations didn't fail.

Fred

Fred,

If I don't have any validations, then it must be that one of the parameters I am trying to set in the model doesn't comport with what the database is looking for I assume? Thanks, RVince

in MyAController I want to create an instance of MyBModel, and have it insert into the table for the MyB models

So, in a function in MyAController, I have

MyB myB = MyB.new myB.something = whatever myB.save

Shouldn't that have inserted a new row into the table for MyB models?

Test the return value from save to see if it succeeded. Look in the log (log/development.log) to see if any errors there.

Colin

Ah, right..it really IS so much easier. Sorry for the dumb Q's here guys, been stuck in silly Javaland with the day job, and not thinking the RubyRails-sense properly here. -Rvince

RVince wrote:

Fred,

If I don't have any validations, then it must be that one of the parameters I am trying to set in the model doesn't comport with what the database is looking for I assume? Thanks, RVince

Instead of assuming, look at your log.

Best,

It doesn;t save and the log reads:

NoMethodError (undefined method `MyB' for #<MyAController:0x664f93c>):   app/controllers/mya_controller.rb:174:in `cxl'

But I DO have a MyB model:

class MyB < ActiveRecord::Base end

as well as a MyBsController:

class MyBsController < ApplicationController end

Im confused regarding ActiveRecord here -- I'm just trying to add a record into the table associate with MyB from MyAsController. -RVince

It doesn;t save and the log reads:

NoMethodError (undefined method `MyB' for #<MyAController:0x664f93c>): app/controllers/mya_controller.rb:174:in `cxl'

You could have said that first time round. Your code reads

MyB myB = MyB.new

I assume you're more familiar with a language where you have to type and declare variables and this is what you're doing. ruby doesn't do that though (and probably parses it as MyB(myB = MyB.new)

Fred

It doesn;t save and the log reads:

NoMethodError (undefined method `MyB' for #<MyAController:0x664f93c>): app/controllers/mya_controller.rb:174:in `cxl'

If you look at the error carefully you will see that it is expecting MyB to be a method of MyAcController for some reason. Have a look at the code around line 174 and work out what MyB is supposed to be at that point. If you cannot see the problem show us the code around the failure. I notice that it is MyAController, I would expect that to be MyAsController but that may not be related to this problem.

Colin

Guys, Ok here is the code (I changed the class names and simplified in what I sent earlier -- here is the exact code) class ChannelsController < ApplicationController ..   def cxl     boxchangedata     offToServer params['id'].to_i,current_associate.username, " ", "Cxl",params['channelnotes']   puts params['cxlidx']   if params['cxlidx'] == "0"     Channelnote cnote = Channelnote.new     cnote.note = params['channelnotes']     cnote.channel_id = @channel.id     cnote.associate_id = current_associate.id     g=cnote.save     puts "***"+g.to_s   end     render :partial => 'channelnotesfield' # to repop the box   end ... end

(Channelnote is MyB in the previous). Here is the (exact) error I am getting from the log:

NoMethodError (undefined method `Channelnote' for #<ChannelsController: 0x671df1c>):   app/controllers/channels_controller.rb:170:in `cxl'

Thanks for looking at this for me guys -- I'm just confused about why this won't write a Channelnote record here. -Rvince

Dude, this isn't Java. :slight_smile:

   cnote = Channelnote.new

Guys, Ok here is the code (I changed the class names and simplified in what I sent earlier -- here is the exact code) class ChannelsController < ApplicationController .. def cxl boxchangedata offToServer params['id'].to_i,current_associate.username, " ", "Cxl",params['channelnotes'] puts params['cxlidx'] if params['cxlidx'] == "0" Channelnote cnote = Channelnote.new

As Fred pointed out this is not C++, it should be cnote = Channelnote.new I kept doing that at first also.

           cnote\.note = params\[&#39;channelnotes&#39;\]
           cnote\.channel\_id = @channel\.id
           cnote\.associate\_id = current\_associate\.id

The usual way to do this sort of thing is using the ActiveRecord relationships, then Rails does all this messy stuff for you. You should only very rarely have to mess about with id values. See the rails guide on ActiveRecord relationships.

Colin

Thanks so much guys! Damn...I'm getting up at 5 am to work on Rails and learn it and do a project in it, but I'm then 9 hours at the ole day job (working on the ole railroad...) writing Java hell.

THere's a Federal Building here in town, populated with a bunch of guys in their 60s with thinning hair and ponytails, writing cobol. If I just stick with Java I will become one of them! Which is why I am forcing myself to become rails-proficient (because as I say, Struts if for guys who don't know Rails under JRuby.)

Thanks again. -RVince