Re: [Rails] Re: A custom method for models
On 15 October 2012 10:30, Soichi Ishida <lists@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
> 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home