Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
A custom method for models
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Soichi Ishida  
View profile  
 More options Oct 15 2012, 3:09 am
From: Soichi Ishida <li...@ruby-forum.com>
Date: Mon, 15 Oct 2012 09:08:06 +0200
Local: Mon, Oct 15 2012 3:08 am
Subject: A custom method for models
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.

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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Colin Law  
View profile  
 More options Oct 15 2012, 4:03 am
From: Colin Law <clan...@googlemail.com>
Date: Mon, 15 Oct 2012 09:02:31 +0100
Local: Mon, Oct 15 2012 4:02 am
Subject: Re: [Rails] A custom method for models
On 15 October 2012 08:08, Soichi Ishida <li...@ruby-forum.com> wrote:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Soichi Ishida  
View profile  
 More options Oct 15 2012, 5:32 am
From: Soichi Ishida <li...@ruby-forum.com>
Date: Mon, 15 Oct 2012 11:30:41 +0200
Local: Mon, Oct 15 2012 5:30 am
Subject: Re: A custom method for models

>   belongs_to :destination, :class_name => "Place", :foreign_key =>
> "destination_id"
>   belongs_to :departure,   :class_name => "Place", :foreign_key =>
> "departure_id"

I didn't know anything about ":foreign_key" option.

I put

class Place < ActiveRecord::Base
  has_many :flight_names, :foreign_key => :departure_id
  has_many :flight_names, :foreign_key => :destination_id
end

class FlightName < ActiveRecord::Base
  belongs_to :departure, :class_name => "Place", :foreign_key =>
:departure_id
  belongs_to :destination, :class_name => "Place", :foreign_key =>
:destination_id
...

It works fine!  Thanks Colin as always!

soichi

--
Posted via http://www.ruby-forum.com/.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Colin Law  
View profile  
 More options Oct 15 2012, 6:43 am
From: Colin Law <clan...@googlemail.com>
Date: Mon, 15 Oct 2012 11:42:12 +0100
Local: Mon, Oct 15 2012 6:42 am
Subject: Re: [Rails] Re: A custom method for models
On 15 October 2012 10:30, Soichi Ishida <li...@ruby-forum.com> wrote:

>>   belongs_to :destination, :class_name => "Place", :foreign_key =>
>> "destination_id"
>>   belongs_to :departure,   :class_name => "Place", :foreign_key =>
>> "departure_id"

> I didn't know anything about ":foreign_key" option.

> I put

> class Place < ActiveRecord::Base
>   has_many :flight_names, :foreign_key => :departure_id
>   has_many :flight_names, :foreign_key => :destination_id

That won't work properly when you come to do place.flight_names,
though the code you have so far may be ok.  You did not look at the
examples in the guide carefully enough.  Check out section 2.10 and
you will see that you need something like
has_many :departure_flight_names, :class_name => "FlightName",
:foreign_key => :departure_id
has_many :destination_flight_names, :class_name => "FlightName",
:foreign_key => :destination_id
then you can say place.departure_flight_names and place.destination_flight_names

Though I am not sure about your choice of class name as FlightName.
Should it not just be Flight or something similar?

Colin


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Soichi Ishida  
View profile  
 More options Oct 17 2012, 1:23 am
From: Soichi Ishida <li...@ruby-forum.com>
Date: Wed, 17 Oct 2012 07:22:16 +0200
Local: Wed, Oct 17 2012 1:22 am
Subject: Re: Re: A custom method for models

> That won't work properly when you come to do place.flight_names,
> though the code you have so far may be ok.  You did not look at the
> examples in the guide carefully enough.  Check out section 2.10 and
> you will see that you need something like
> has_many :departure_flight_names, :class_name => "FlightName",
> :foreign_key => :departure_id
> has_many :destination_flight_names, :class_name => "FlightName",
> :foreign_key => :destination_id
> then you can say place.departure_flight_names and
> place.destination_flight_names

Rails is amazingly fun to deal with...Now I have learned a lot. Thanks.

> Though I am not sure about your choice of class name as FlightName.
> Should it not just be Flight or something similar?

FlightName only contains integer identification codes, and Flight
includes Airline.company and Date as well, which are not apparent in
this thread. So it's OK.
Thanks, though.

soichi

--
Posted via http://www.ruby-forum.com/.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »