On 15 October 2012 08:08, Soichi Ishida <
li...@ruby-forum.com> wrote:
> Rails 3.1.3
>
> Say, I have models and associations like
> models:
> Plan :flight_name_id: integer
>
> FlightName :departure_id :integer
> :destination_id :integer
>
> Place :city_id :integer
>
> City :name :string
>
>
> Plan 1--n FlightName n--1 Place n--1 City
>
>
> Apparently, a flight_name has connections to TWO places, where one of
> them refers to departure_id and another to destination_id.
> Corresponding
place.id's are stored in them.
Show us the class definitions with has_many and belongs_to
specifications. If the problem is that you do not know how to specify
two places in the flight name then you need to do something like
class Flightname
belongs_to :destination, :class_name => "Place", :foreign_key =>
"destination_id"
belongs_to :departure, :class_name => "Place", :foreign_key =>
"departure_id"
Then you can say flightname.destination and flightname.departure. You
also have to put two equivalent has_many definitions in class Place.
Have a look at the rails guide on activerecord associations and the
rails docs for more details.
Colin
>
> In a view, I would like to generate City.name's (string) for both
> departure place and destination place.
>
> My question is: How can I achieve this?
>
> in a view (html.erb template)
>
> plan.flight_name.plan
>
> gives an error, "undefined method `place' for" obviously.
> I am guessing that a custom method needs to be defined in order to pull
> out City,name from a Plan.
>
> Can anyone give me advice?
>
> soichi
>
> --
> Posted via
http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
rubyonrails-ta...@googlegroups.com.
> For more options, visit
https://groups.google.com/groups/opt_out.
>
>