# Suggestions on Ruby code for API

**URL:** <https://www.ruby-forum.com/t/suggestions-on-ruby-code-for-api/194438>\
**Category:** Rails\
**Created:** [September 6, 2010, 11:26am UTC](https://www.ruby-forum.com/t/suggestions-on-ruby-code-for-api/194438 "2010-09-06T11:26:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![arunachandu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@arunachandu](https://www.ruby-forum.com/u/arunachandu)\
**Post date:** [September 6, 2010, 11:26am UTC](https://www.ruby-forum.com/t/suggestions-on-ruby-code-for-api/194438/1 "2010-09-06T11:26:08Z")

</div>

hi , i am new to ROR

i was acually trying to write Ruby code for the api blogs

i have written the ruby code as

```
  Loading development environment (Rails 2.3.8)
  >> class Blogpost < ActiveRecord::Base
  >> has_many :taggings
  >> has_many :tags,:through => :taggings
  >> end
  => nil
 >> class Taggings < ActiveRecord::Base
 >> belongs_to :blogpost
>> belongs_to :tag
>> end
=> nil

?> @tags=Tag.find_by_name("blog7")
=> #<Tag id: 4, name: "blog7">
>> @taggings=Tagging.find_by_id(@tags.id)
=> #<Tagging id: 4, tag_id: 1, taggable_id: 4, taggable_type:

```

“Blogpost”, created\_at: “2010-09-02 10:03:08”\>  
\>\> @blogposts=Blogpost.find\_by\_id(@taggings.taggable\_id)  
=\> #\<Blogpost id: 4, title: “blog post4”, type: nil, slug:  
“blog-post4”, description: “

BLOG desc 4

”, meta: nil, user\_id: 1,  
category\_id: 379344121, times\_viewed: 2, comments\_count: 0, published:  
1, created\_at: “2010-09-02 10:03:08”, updated\_at: “2010-09-03 05:11:46”,  
delta: false\>

Whether this way of writing Ruby code is the correct procedure for API…

Please give suggestions …

---

<div class="post-metadata">

**Author:** ![arunachandu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@arunachandu](https://www.ruby-forum.com/u/arunachandu)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/suggestions-on-ruby-code-for-api/194438/2 "2010-09-06T16:39:38Z")

</div>

On Sep 6, 10:26 am, Aruna C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hi , i am new to ROR
> 
> i was acually trying to write Ruby code for the api blogs
> 
> i have written the ruby code as
> 
> ```
> Loading development environment (Rails 2.3.8)
> >> class Blogpost < ActiveRecord::Base
> >> has_many :taggings
> >> has_many :tags,:through => :taggings
> >> end
> => nil
> >> class Taggings < ActiveRecord::Base
> >> belongs_to :blogpost
> >> belongs_to :tag
> >> end
> => nil
> 
> ?> @tags=Tag.find_by_name("blog7")
> => #<Tag id: 4, name: "blog7">
> >> @taggings=Tagging.find_by_id(@tags.id)
> => #<Tagging id: 4, tag_id: 1, taggable_id: 4, taggable_type:
> 
> ```
> 
> “Blogpost”, created\_at: “2010-09-02 10:03:08”\>  
> \>\> @blogposts=Blogpost.find\_by\_id(@taggings.taggable\_id)  
> =\> #\<Blogpost id: 4, title: “blog post4”, type: nil, slug:  
> “blog-post4”, description: “
> 
> BLOG desc 4
> 
> ”, meta: nil, user\_id: 1,  
> category\_id: 379344121, times\_viewed: 2, comments\_count: 0, published:  
> 1, created\_at: “2010-09-02 10:03:08”, updated\_at: “2010-09-03 05:11:46”,  
> delta: false\>
> 
> Whether this way of writing Ruby code is the correct procedure for API…

If you had those associations then typically you’d use them, ie you’d  
do stuff like @tag.taggings or @tag.blogposts rather than calling  
find. It’s also a little misleading the way you’ve used plurals  
(@tags, @taggings etc…) for things that are only single objects, not  
collections.

Fred
