Re: [Rails] Saving a nested object to a whole collection of objects at once.

2016-10-18 Thread Colin Law
On 18 October 2016 at 15:47, Chris Vukin  wrote:

> thanks for the heads up, I'll change the methods to use the @plant.farm
>
> We'd like to apply the same feeding to all plants in a farm.. so maybe
> something along the lines of: @farm.plants.feed ? We want to be able to
> feed all the plants in the farm with the same feed(like a saved recipe for
> a feeding mix), does this make more sense? So if we have a farm with id of
> 1 and plant's with id 1-16 belong to that farm, we then want to be able to
> apply the fame feeding object to all plants in that farm..
>


If you have the situation where each plant can have many feed records, but
also one feed record may be associated with many plants then you need a
many to many relationship, so plant has_and_belongs_to_many feeds. See the
rails guide on activerecord associations for how that works.

Colin


>
> On Tuesday, October 18, 2016 at 10:00:22 AM UTC-4, Colin Law wrote:
>>
>> On 18 October 2016 at 14:27, Chris Vukin  wrote:
>>
>>> thank you for your reply Colin, here's what we're trying to do:
>>>
>>> The app has a farm model and each farm has nested plant models, nested
>>> under each plant is a feeding model. Say a farm has 16 plants, we'd like to
>>> be able to feed all the plants at once and not create a new feeding for
>>> each plant but retain the ability to create an inidividual feeding under a
>>> specific plant.
>>>
>>> currently the new feed method looks like this:
>>> def new
>>> @farm = Farm.find(params[:farm_id])
>>> @plant = Plant.find(params[:plant_id])
>>>
>>
>>
>> Don't do that, you leave open the possibility of fetching the wrong farm.
>> Use
>> @farm = @plant.farm
>> in fact you don't even need @farm, just use @plant.farm when you need it
>> in the view. Also the same in the create method below.
>>
>> Back to the question, I don't understand what you mean by "feed all the
>> plants at once". Do you mean you want to update all the feeds for a plant?
>> If so then you can do something like
>> @plant.feeds.each do |feed|
>>   do something with feed
>>   feed.save
>> end
>>
>> Colin
>>
>>
>>> @feed = @plant.feeds.new
>>>   end
>>>
>>> and the create method for feeds:
>>>
>>> def create
>>>
>>> @farm = Farm.find(params[:farm_id])
>>> @plant = Plant.find(params[:plant_id] )
>>> @feed = @plant.feeds.new(feed_params)
>>>
>>> respond_to do |format|
>>>   if @feed.save
>>> format.html { redirect_to farm_plant_feeds_path(@farm, @plant,
>>> @feeds), notice: 'feed was successfully created.' }
>>> format.json { render :show, status: :created, location:
>>> farm_plant_feeds_path(@farm, @plant, @feeds)}
>>>   else
>>> format.html { render :new }
>>> format.json { render json: @feed.errors, status:
>>> :unprocessable_entity }
>>>   end
>>> end
>>>   end
>>>
>>> On Tuesday, October 18, 2016 at 3:08:43 AM UTC-4, Colin Law wrote:

 On 18 October 2016 at 00:29, Chris Vukin  wrote:

> Hello, I'm wondering about how to accomplish saving the same nested
> form on an entire collection of objects that the form is nested under.. 
> I'm
> not finding much when googling around, is this something that is possible
> with rails?
>

 Could you give more details of what you want to do? Give us an example
 of models, associations and what is to be achieved.

 Colin


>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Ruby on Rails: Talk" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to rubyonrails-ta...@googlegroups.com.
>>> To post to this group, send email to rubyonra...@googlegroups.com.
>>> To view this discussion on the web visit https://groups.google.com/d/ms
>>> gid/rubyonrails-talk/836fa4b2-9900-4cb2-9766-4045ac6864f3%
>>> 40googlegroups.com
>>> 
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-talk+unsubscr...@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/rubyonrails-talk/5428c01d-e798-4004-b58e-
> 2daf2cc7eda5%40googlegroups.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and 

Re: [Rails] Saving a nested object to a whole collection of objects at once.

2016-10-18 Thread Chris Vukin
thanks for the heads up, I'll change the methods to use the @plant.farm

We'd like to apply the same feeding to all plants in a farm.. so maybe 
something along the lines of: @farm.plants.feed ? We want to be able to 
feed all the plants in the farm with the same feed(like a saved recipe for 
a feeding mix), does this make more sense? So if we have a farm with id of 
1 and plant's with id 1-16 belong to that farm, we then want to be able to 
apply the fame feeding object to all plants in that farm..

On Tuesday, October 18, 2016 at 10:00:22 AM UTC-4, Colin Law wrote:
>
> On 18 October 2016 at 14:27, Chris Vukin  
> wrote:
>
>> thank you for your reply Colin, here's what we're trying to do:
>>
>> The app has a farm model and each farm has nested plant models, nested 
>> under each plant is a feeding model. Say a farm has 16 plants, we'd like to 
>> be able to feed all the plants at once and not create a new feeding for 
>> each plant but retain the ability to create an inidividual feeding under a 
>> specific plant.
>>
>> currently the new feed method looks like this:
>> def new
>> @farm = Farm.find(params[:farm_id])
>> @plant = Plant.find(params[:plant_id])
>>
>
>
> Don't do that, you leave open the possibility of fetching the wrong farm. 
> Use
> @farm = @plant.farm
> in fact you don't even need @farm, just use @plant.farm when you need it 
> in the view. Also the same in the create method below.
>
> Back to the question, I don't understand what you mean by "feed all the 
> plants at once". Do you mean you want to update all the feeds for a plant? 
> If so then you can do something like
> @plant.feeds.each do |feed|
>   do something with feed
>   feed.save
> end
>
> Colin
>  
>
>> @feed = @plant.feeds.new
>>   end
>>
>> and the create method for feeds:
>>
>> def create
>>
>> @farm = Farm.find(params[:farm_id])
>> @plant = Plant.find(params[:plant_id] )
>> @feed = @plant.feeds.new(feed_params)
>>
>> respond_to do |format|
>>   if @feed.save
>> format.html { redirect_to farm_plant_feeds_path(@farm, @plant, 
>> @feeds), notice: 'feed was successfully created.' }
>> format.json { render :show, status: :created, location: 
>> farm_plant_feeds_path(@farm, @plant, @feeds)}
>>   else
>> format.html { render :new }
>> format.json { render json: @feed.errors, status: 
>> :unprocessable_entity }
>>   end
>> end
>>   end
>>
>> On Tuesday, October 18, 2016 at 3:08:43 AM UTC-4, Colin Law wrote:
>>>
>>> On 18 October 2016 at 00:29, Chris Vukin  wrote:
>>>
 Hello, I'm wondering about how to accomplish saving the same nested 
 form on an entire collection of objects that the form is nested under.. 
 I'm 
 not finding much when googling around, is this something that is possible 
 with rails?

>>>
>>> Could you give more details of what you want to do? Give us an example 
>>> of models, associations and what is to be achieved.
>>>
>>> Colin
>>>  
>>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Ruby on Rails: Talk" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to rubyonrails-ta...@googlegroups.com .
>> To post to this group, send email to rubyonra...@googlegroups.com 
>> .
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/rubyonrails-talk/836fa4b2-9900-4cb2-9766-4045ac6864f3%40googlegroups.com
>>  
>> 
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/5428c01d-e798-4004-b58e-2daf2cc7eda5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] Saving a nested object to a whole collection of objects at once.

2016-10-18 Thread Colin Law
On 18 October 2016 at 14:27, Chris Vukin  wrote:

> thank you for your reply Colin, here's what we're trying to do:
>
> The app has a farm model and each farm has nested plant models, nested
> under each plant is a feeding model. Say a farm has 16 plants, we'd like to
> be able to feed all the plants at once and not create a new feeding for
> each plant but retain the ability to create an inidividual feeding under a
> specific plant.
>
> currently the new feed method looks like this:
> def new
> @farm = Farm.find(params[:farm_id])
> @plant = Plant.find(params[:plant_id])
>


Don't do that, you leave open the possibility of fetching the wrong farm.
Use
@farm = @plant.farm
in fact you don't even need @farm, just use @plant.farm when you need it in
the view. Also the same in the create method below.

Back to the question, I don't understand what you mean by "feed all the
plants at once". Do you mean you want to update all the feeds for a plant?
If so then you can do something like
@plant.feeds.each do |feed|
  do something with feed
  feed.save
end

Colin


> @feed = @plant.feeds.new
>   end
>
> and the create method for feeds:
>
> def create
>
> @farm = Farm.find(params[:farm_id])
> @plant = Plant.find(params[:plant_id] )
> @feed = @plant.feeds.new(feed_params)
>
> respond_to do |format|
>   if @feed.save
> format.html { redirect_to farm_plant_feeds_path(@farm, @plant,
> @feeds), notice: 'feed was successfully created.' }
> format.json { render :show, status: :created, location:
> farm_plant_feeds_path(@farm, @plant, @feeds)}
>   else
> format.html { render :new }
> format.json { render json: @feed.errors, status:
> :unprocessable_entity }
>   end
> end
>   end
>
> On Tuesday, October 18, 2016 at 3:08:43 AM UTC-4, Colin Law wrote:
>>
>> On 18 October 2016 at 00:29, Chris Vukin  wrote:
>>
>>> Hello, I'm wondering about how to accomplish saving the same nested form
>>> on an entire collection of objects that the form is nested under.. I'm not
>>> finding much when googling around, is this something that is possible with
>>> rails?
>>>
>>
>> Could you give more details of what you want to do? Give us an example of
>> models, associations and what is to be achieved.
>>
>> Colin
>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-talk+unsubscr...@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/rubyonrails-talk/836fa4b2-9900-4cb2-9766-
> 4045ac6864f3%40googlegroups.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvikexavJw3EKpciDmUwBLN9Rxy%2BBxkKe%3Dm0r%2Bo9skbeg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] Saving a nested object to a whole collection of objects at once.

2016-10-18 Thread Chris Vukin
thank you for your reply Colin, here's what we're trying to do:

The app has a farm model and each farm has nested plant models, nested 
under each plant is a feeding model. Say a farm has 16 plants, we'd like to 
be able to feed all the plants at once and not create a new feeding for 
each plant but retain the ability to create an inidividual feeding under a 
specific plant.

currently the new feed method looks like this:
def new
@farm = Farm.find(params[:farm_id])
@plant = Plant.find(params[:plant_id])
@feed = @plant.feeds.new
  end

and the create method for feeds:

def create

@farm = Farm.find(params[:farm_id])
@plant = Plant.find(params[:plant_id] )
@feed = @plant.feeds.new(feed_params)

respond_to do |format|
  if @feed.save
format.html { redirect_to farm_plant_feeds_path(@farm, @plant, 
@feeds), notice: 'feed was successfully created.' }
format.json { render :show, status: :created, location: 
farm_plant_feeds_path(@farm, @plant, @feeds)}
  else
format.html { render :new }
format.json { render json: @feed.errors, status: 
:unprocessable_entity }
  end
end
  end

On Tuesday, October 18, 2016 at 3:08:43 AM UTC-4, Colin Law wrote:
>
> On 18 October 2016 at 00:29, Chris Vukin  
> wrote:
>
>> Hello, I'm wondering about how to accomplish saving the same nested form 
>> on an entire collection of objects that the form is nested under.. I'm not 
>> finding much when googling around, is this something that is possible with 
>> rails?
>>
>
> Could you give more details of what you want to do? Give us an example of 
> models, associations and what is to be achieved.
>
> Colin
>  
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/836fa4b2-9900-4cb2-9766-4045ac6864f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] Saving a nested object to a whole collection of objects at once.

2016-10-18 Thread Colin Law
On 18 October 2016 at 00:29, Chris Vukin  wrote:

> Hello, I'm wondering about how to accomplish saving the same nested form
> on an entire collection of objects that the form is nested under.. I'm not
> finding much when googling around, is this something that is possible with
> rails?
>

Could you give more details of what you want to do? Give us an example of
models, associations and what is to be achieved.

Colin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuLmaHVJne_akHB8Vt0PBbqqw9ceo9hczRK0MsFPKfuPw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.