[Rails] Re: accessing from both ends of has many through relationship

2012-08-03 Thread Martyn W.
For <%= work_ticket.material.number %>

I get undefined method `number' for nil:NilClass

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[Rails] accessing from both ends of has many through relationship

2012-08-03 Thread Martyn W.
I have a 'has many through' relationship in my models. I am trying to
access objects from either side of this relationship, with mixed
results.Here are my models:

class Material < ActiveRecord::Base
  attr_accessible :description, :number
  has_many :parts
  has_many :work_tickets, :through => :parts
end

class Part < ActiveRecord::Base
  belongs_to :material
  attr_accessible :description, :number, :yield, :material_id
  has_many :work_tickets
  has_many :operations, :dependent => :destroy
end

class WorkTicket < ActiveRecord::Base
  belongs_to :part
  belongs_to :material
  attr_accessible :number, :yield, :opened, :closed, :part_id
  has_many :works, :dependent => :destroy
end

I can access the work_tickets from the material with:

@work_tickets = @material.work_tickets

But, cannot access material from work_ticket:

<%= work_ticket.material.number %>

Forcing me to use:

<%= work_ticket.part.material.number %>

Am I expecting the wrong behaviour, or am I using the wrong relationship
pattern?

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[Rails] Re: Routing error: no route matches contoller works. Route exists though

2012-06-05 Thread Martyn W.
Martyn W. wrote in post #1063175:
> I have a fairly straight forward rails app, version 3.2. When I try to
> create a Work instance, with deliberately invalid fields from a form, I
> get a routing error:
>
> No route matches {:controller=>"works"}
>
> This does not happen when I try the same test with an update on a Work
> instance. After an attempted update I get the edit.html.erb page
> rendered with the invalid fields highlighted.
>
> I dont understand why after failing the save in the controller and
> trying to render "new" it would give me a route matching error. Any
> ideas?
>
> Attached are my model, controller and routes.rb

So, it was a link in the new.htnl.erb file referencing a list of works 
through the associated workticket. I guess I need to reassign the 
workticket parameter for the failed save render.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.



[Rails] Re: Routing error: no route matches contoller works. Route exists though

2012-06-05 Thread Martyn W.
Hassan Schroeder wrote in post #1063180:
> On Tue, Jun 5, 2012 at 9:56 AM, Martyn W.  wrote:
>
>> I dont understand why after failing the save in the controller and
>> trying to render "new" it would give me a route matching error. Any
>> ideas?
>
> Yes - look at the log for that request :-)
>
> --
> Hassan Schroeder  hassan.schroe...@gmail.com
> http://about.me/hassanschroeder
> twitter: @hassan

Hassan,

I don't see anything in the logs that explains the routing error. The 
controller is redirecting the failed save to the "new" view using 
render. There shouldn't be any routing issues here.

Here is the log entry for this action: (sanitized a bit to read)

Started POST "/works" for 127.0.0.1 at 2012-06-05 12:04:18 -0500
Processing by WorksController#create as HTML
Parameters: {"utf8"=>"?", 
"authenticity_token"=>"vrAsdpq9G04to6SgW5HNvb5NWcN8rPCy01ZwKK95lhM=", 
"work"=>{"start(1i)"=>"2012", "start(2i)"=>"6", "start(3i)"=>"5", 
"start(4i)"=>"17", "start(5i)"=>"04", "end(1i)"=>"2012", "end(2i)"=>"6", 
"end(3i)"=>"5", "end(4i)"=>"17", "end(5i)"=>"04", "good"=>"", "bad"=>"", 
"operation"=>"1", "work_ticket_id"=>"8", "shift_id"=>"1", 
"operator_id"=>"1", "machine_id"=>"1"}, "commit"=>"Create Work"}
In start_must_be_before_end_time
In start_must_be_before_end_time
In start_must_be_before_end_time
In start_must_be_before_end_time
@work.save: false
@work.errors.count: 2

  Rendered works/_form.html.erb (62.5ms)
  Rendered works/new.html.erb within layouts/application (62.5ms)
Completed 500 Internal Server Error in 156ms

ActionController::RoutingError (No route matches 
{:controller=>"works"}):
  app/views/works/new.html.erb:5:in 
`_app_views_works_new_html_erb__545664482_19220532'
  app/controllers/works_controller.rb:136:in `block (2 levels) in 
create'
  app/controllers/works_controller.rb:127:in `create'


  Rendered 
c:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb
 
within rescues/layout (0.0ms)

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.



[Rails] Routing error: no route matches contoller works. Route exists though

2012-06-05 Thread Martyn W.
I have a fairly straight forward rails app, version 3.2. When I try to
create a Work instance, with deliberately invalid fields from a form, I
get a routing error:

No route matches {:controller=>"works"}

This does not happen when I try the same test with an update on a Work
instance. After an attempted update I get the edit.html.erb page
rendered with the invalid fields highlighted.

I dont understand why after failing the save in the controller and
trying to render "new" it would give me a route matching error. Any
ideas?

Attached are my model, controller and routes.rb

Attachments:
http://www.ruby-forum.com/attachment/7456/routes.rb
http://www.ruby-forum.com/attachment/7457/work.rb
http://www.ruby-forum.com/attachment/7458/works_controller.rb


-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.



[Rails] Re: Re: validation not called on create, only update

2012-06-04 Thread Martyn W.
Martyn W. wrote in post #1063043:
> Michael Pavling wrote in post #1062778:
>> On 1 June 2012 04:31, flaps  wrote:
>>> THanks for your response. The validation methods are not called on a
>>> create, allowing the save method to be called, unprotected. I dont
>>> want the save to be called let alone fail.
>>
>> Is there any chance you could post your controller code. I wonder
>> whether you've got a ".save false" in the create action...
>
> Here is the create code in the controller:
>
>   # POST /works
>   # POST /works.json
>   def create
> @work = Work.new(params[:work])
>   @operations = @work.work_ticket.part.operations
> respond_to do |format|
>   if @work.save
> format.html { redirect_to @work, notice: 'Work was successfully
> created.' }
> format.json { render json: @work, status: :created, location:
> @work }
>   else
> format.html { render action: "new" }
> format.json { render json: @work.errors, status:
> :unprocessable_entity }
>   end
> end
>   end

It looks like the validation is called, it is the render after the 
failed fail that errors out now. I get the following error in the 
browser:

Routing Error

No route matches {:controller=>"works"}

Try running rake routes for more information on available routes.

All the routes for Work are working, except in this case after a failed 
invalid save.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.



[Rails] Re: Re: validation not called on create, only update

2012-06-04 Thread Martyn W.
Michael Pavling wrote in post #1062778:
> On 1 June 2012 04:31, flaps  wrote:
>> THanks for your response. The validation methods are not called on a
>> create, allowing the save method to be called, unprotected. I dont
>> want the save to be called let alone fail.
>
> Is there any chance you could post your controller code. I wonder
> whether you've got a ".save false" in the create action...

Here is the create code in the controller:

  # POST /works
  # POST /works.json
  def create
@work = Work.new(params[:work])
  @operations = @work.work_ticket.part.operations
respond_to do |format|
  if @work.save
format.html { redirect_to @work, notice: 'Work was successfully 
created.' }
format.json { render json: @work, status: :created, location: 
@work }
  else
format.html { render action: "new" }
format.json { render json: @work.errors, status: 
:unprocessable_entity }
  end
end
  end

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.