Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Child might not be named correctly #28

Closed
fourcolors opened this issue May 24, 2011 · 12 comments
Closed

Child might not be named correctly #28

fourcolors opened this issue May 24, 2011 · 12 comments

Comments

@fourcolors
Copy link

Hey,

I'm not sure if this is the expected behavior but it threw me off a little bit when I first ran into it so I thought I would post it here.

I have a tour_stop that has two tour_routes. This is what my models look like.

class TourRoute < ActiveRecord::Base

  #Associations
  belongs_to :end, :class_name => "TourStop"
  belongs_to :start, :class_name => "TourStop"

end

class TourStop < ActiveRecord::Base

  #Associations
  belongs_to :tour
  belongs_to :location
  has_one :route_starting_here, :class_name => "TourRoute", :foreign_key => "start_id"
  has_one :route_ending_here, :class_name => "TourRoute", :foreign_key => "end_id"

end

Ok cool. Now in my awesome api::tour view I have something like this

object @tour
attributes :created_at, :days_required, :description, :fee_description, :fee_scale, :name, :published, :updated_at, :lat, :lng

node :small_image_url do |tour|
 @root_url + tour.small_image_url
end

node :large_image_url do |tour|
 @root_url + tour.large_image_url
end


child :tour_stops do
  attributes :position, :updated_at, :created_at
  child :location do 
    attributes :name, :description, :location_type, :lat, :lng, :address, :city, :state, :postal_code, :phone, :email, :url, :fee_scale, :fee_description, :availability_description, :created_at, :updated_at
  end

  child :route_starting_here  do 
    attribute :route
  end

  child :route_ending_here do 
    attribute :route
  end
end

The part to look at here, is the child :route_starting_here portion. If I leave it as is, the json produced looks like this

{
    "tour": {
        "created_at": "2011-05-20T22:01:37Z",
        "days_required": null,
        "description": "This is the description for the grand canyon tour",
        "fee_description": null,
        "fee_scale": 1,
        "name": "Grand Canyon Tour",
        "published": true,
        "updated_at": "2011-05-20T22:01:37Z",
        "lat": 39,
        "lng": -111,
        "small_image_url": "http://tour_builder.dev/uploads/tour/image/1/small_test.gif",
        "large_image_url": "http://tour_builder.dev/uploads/tour/image/1/large_test.gif",
        "tour_stops": [
            {
                "tour_stop": {
                    "position": 1,
                    "updated_at": "2011-05-20T22:12:39Z",
                    "created_at": "2011-05-20T22:12:39Z",
                    "location": {
                        "name": "grand canyon store",
                        "description": "grand canyon store is a way awesome place",
                        "location_type": "museum",
                        "lat": 39,
                        "lng": -111,
                        "address": null,
                        "city": null,
                        "state": null,
                        "postal_code": null,
                        "phone": null,
                        "email": null,
                        "url": null,
                        "fee_scale": 1,
                        "fee_description": null,
                        "availability_description": null,
                        "created_at": "2011-05-20T22:12:11Z",
                        "updated_at": "2011-05-20T22:12:11Z"
                    },
                    "tour_route": {
                        "route": {
                            "type": "LineString",
                            "coordinates": [
                                [
                                    0,
                                    0
                                ],
                                [
                                    1,
                                    1
                                ],
                                [
                                    2,
                                    1
                                ],
                                [
                                    2,
                                    2
                                ]
                            ]
                        }
                    }
                }
            }
        ]
    }
}

Notice that tour_route is not being named :tour_starting_here.. it's being named "tour_route". In order for me to get it named correctly to :tour_starting_here I need to set it's alias like ...

  child :route_starting_here => :route_starting_here do 
    attribute :route
  end

Which will correctly produce

"route_starting_here": {
                        "route": {.......... other stuff blah blah blah

Shouldn't this all ready know that I named this route_starting_here? It seems like it's pulling from the class name to set the name vs the association name which imo would make more since. Anyway, just a thought. Thanks for any input.

@nesquena
Copy link
Owner

Yes, I do see your point, it is introspecting the class name while it could just use the name provided. I will look into fixing this sometime soon.

@fourcolors
Copy link
Author

Ok awesome. I'll see if I can't hack something together also. Although it might break more than it fixed ha, jk.

@nesquena
Copy link
Owner

@nesquena
Copy link
Owner

[Note to self] Make sure this fix, fixes this issue as well: #29

@nesquena
Copy link
Owner

(Heads up) Might be a few days until I have a chance to get to this since the workaround is easy but obviously pull requests welcome :)

@fourcolors
Copy link
Author

No worries. That will give me a chance to try and create something ha. I'm like a yellow belt in code fu.

@nesquena
Copy link
Owner

Going to close this, not sure it's still relevant. If it is, open a new issue.

@fourcolors
Copy link
Author

Thanks. I actually forgot about this ticket! I'm sorry I didn't actually work on it. That was pretty irresponsible on my part. I did look at the source but ended up kind of scratching my head. I'm still trying to get used to open source projects. Anyway thanks for the project, it's a life saver.

:/

@akre54
Copy link

akre54 commented Dec 3, 2012

@nesquena would you mind re-opening this ticket? I bumped up against this same issue (belongs_to :author, class_name: 'User' necessitates :author => :author in my show.rabl). It's an easy fix in my code, but feels like a hack nonetheless.

@nesquena
Copy link
Owner

nesquena commented Dec 3, 2012

@akre54 I believe this captures it? #330

@akre54
Copy link

akre54 commented Dec 3, 2012

@nesquena nope, no dice.

child @author != child :author => :author, since :author is a relation (comment belongs_to author, author has_many comments) and not an instance variable. Not a huge deal, just a little unexpected to workaround it like this.

@nesquena
Copy link
Owner

nesquena commented Dec 3, 2012

Got it, will take care of both these cases in the future, thanks for reminding me of the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants