# Need help

**URL:** <https://www.ruby-forum.com/t/need-help/118361>\
**Category:** Rails\
**Created:** [October 26, 2007, 3:20am UTC](https://www.ruby-forum.com/t/need-help/118361 "2007-10-26T03:20:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [October 26, 2007, 3:20am UTC](https://www.ruby-forum.com/t/need-help/118361/1 "2007-10-26T03:20:00Z")

</div>

So, I have been out of the rails loop for a little bit, and I’m not sure  
the best “rails way” to do this. I have two objects. ObjectA has\_many  
:object\_bs, and ObjectB belongs\_to :object\_a

In my controller I am trying to do basic CRUD operations. In my view I  
have all the attributes of both ObjectA and ObjectB. So saying that  
ObjectA has a name and id, and ObjectB has a kind and id and  
object\_a\_id.

Basically what I want to do is

@a = Object.new(params[:object\_a]), but when I do that I get an  
undefined method because there are fields for a different object. What I  
was doing is making a new\_thing method which takes the params and goes  
through each one and pulls out the ObjectA attributes and uses those,  
then @a.object\_bs \<\< ObjectB.new(other param stuff).

I know there is a better way, I just need help learning what the is.

Thanks in advance, sorry if it is confusing.

~Jeremy

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [October 27, 2007, 3:36am UTC](https://www.ruby-forum.com/t/need-help/118361/2 "2007-10-27T03:36:58Z")

</div>

Jeremy W. wrote:

> So, I have been out of the rails loop for a little bit, and I’m not sure  
> the best “rails way” to do this. I have two objects. ObjectA has\_many  
> :object\_bs, and ObjectB belongs\_to :object\_a
> 
> In my controller I am trying to do basic CRUD operations. In my view I  
> have all the attributes of both ObjectA and ObjectB. So saying that  
> ObjectA has a name and id, and ObjectB has a kind and id and  
> object\_a\_id.
> 
> Basically what I want to do is
> 
> @a = Object.new(params[:object\_a]), but when I do that I get an  
> undefined method because there are fields for a different object. What I  
> was doing is making a new\_thing method which takes the params and goes  
> through each one and pulls out the ObjectA attributes and uses those,  
> then @a.object\_bs \<\< ObjectB.new(other param stuff).
> 
> I know there is a better way, I just need help learning what the is.
> 
> Thanks in advance, sorry if it is confusing.
> 
> ~Jeremy

Here’s my take. First of all, you needn’t send either of the object ids  
as parameters, as these are automatically taken care of by ActiveRecord.  
And the object\_a\_id attribute of your ObjectB model can be taken care of  
by creating it using the association.

That leaves us with ObjectB’s :kind attribute. I’m not perfectly sure if  
this would work, but you may as well give it a shot and let me know.

In your ObjectA controller:

def create  
@object\_a = ObjectA.create(params[object\_a])  
# … (Various respond\_to’s)  
end

And in your ObjectA class:

class ObjectA  
# I _think_ that this will allow you store a value in the  
# :kind attribute for this object while it exists in memory  
# after being created by the controller. I could be mistaken.  
attr\_accessor :kind

```
# Standard AR callback
after_create :create_object_b

def create_object_b
  # Creating the object_b through the object_a will take
  # care of the has_many/belongs_to relationship.
  self.object_bs.create(:kind => self.kind)
end

```

end

Good luck.
